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 void MakeFilename(char * fn)
20 for(int i=0; i<l; i++)
24 else if (fn[i] >= 'A' && fn[i] <= 'Z')
26 else if (fn[i] >= 'a' && fn[i] <= 'z')
28 else if (fn[i] >= '0' && fn[i] <= '9')
36 int main(int argc, char * argv[])
38 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" };
40 FILE * master = fopen("master-doc.txt", "r");
44 printf("Could not open master doc (master-doc.txt) file!\n");
48 FILE * toc = fopen("_manual/00_toc.html", "w");
52 printf("Could not open TOC file!\n");
57 fprintf(toc, "---\n");
58 fprintf(toc, "title: Ardour Table of Contents\n");
59 fprintf(toc, "---\n\n");
61 char buffer[1024000], keyword[1024], token[1024];
62 char title[1024], shortTitle[1024], inclFile[1024], style[1024];
64 int lineCount = 0, startLine, sectionLineCount;
65 int part = 0, chapter = 0, subchapter = 0;
67 FILE * newFile = NULL;
68 char level1File[1024], level2File[1024], level3File[1024], temp[1024];
69 char partFN[1024], chapterFN[1024], scLink[4096];
73 fgets(buffer, 1023999, master);
76 // Look for start of file marker
77 if (strncmp(buffer, "---", 3) == 0)
83 sectionLineCount = lineCount - startLine;
84 printf(" (%d lines)\n", sectionLineCount);
87 // Reset the "short" title, include file & sytle
94 fgets(buffer, 1023999, master);
97 if (strncmp(buffer, "---", 3) != 0)
99 sscanf(buffer, "%[^:]: %[^\n]", keyword, token);
101 if (strcmp(keyword, "title") == 0)
103 strcpy(title, token);
105 else if (strcmp(keyword, "part") == 0)
107 int len = strlen(token);
112 level = 1, chapter++, subchapter = 0;
114 level = 2, subchapter++;
116 level = -1; // Something went wrong
118 else if (strcmp(keyword, "include") == 0)
120 strcpy(inclFile, token);
122 else if (strcmp(keyword, "menu_title") == 0)
124 strcpy(shortTitle, token);
126 else if (strcmp(keyword, "style") == 0)
128 strcpy(style, token);
131 printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
135 // We hit the end of our keyword block, now do something
138 printf("\nPart %s: ", roman[part]);
140 printf("\n\tCh. %d: ", chapter);
146 if (strlen(inclFile) > 0)
147 printf(" [%s]", inclFile);
149 startLine = lineCount;
153 // Parts & chapters don't have any content...
156 sprintf(level1File, "_manual/%02d_%s", part, temp);
157 mkdir(level1File, 0777);
159 // Set up the "part" level of TOC link
160 sprintf(partFN, "%s", temp);
161 fprintf(toc, "<h2>Part %s: %s</h2>\n", roman[part], title);
163 // Make the file expected at this level...
164 sprintf(temp, "%s.html", level1File);
165 FILE * tfp = fopen(temp, "w");
166 fprintf(tfp, "---\n");
167 fprintf(tfp, "title: %s\n", title);
169 if (strlen(shortTitle) > 0)
170 fprintf(tfp, "menu_title: %s\n", shortTitle);
172 fprintf(tfp, "---\n");
173 fprintf(tfp, "\n{%% children %%}\n\n");
180 sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
181 mkdir(level2File, 0777);
183 // Set up the "chapter" part of the TOC link
184 sprintf(chapterFN, "%s", temp);
185 fprintf(toc, " <p id=chapter>Ch. %d: <a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
187 // Make the file expected at this level...
188 sprintf(temp, "%s.html", level2File);
189 FILE * tfp = fopen(temp, "w");
190 fprintf(tfp, "---\n");
191 fprintf(tfp, "title: %s\n", title);
193 if (strlen(shortTitle) > 0)
194 fprintf(tfp, "menu_title: %s\n", shortTitle);
196 fprintf(tfp, "---\n");
197 fprintf(tfp, "\n{%% children %%}\n\n");
204 sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
206 // Make the file expected at this level...
211 sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
212 fprintf(toc, " <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
214 if (strlen(inclFile) > 0)
216 // Copy the include file to the appropriate spot...
217 sprintf(temp, "include/%s", inclFile);
218 FILE * fileToCopy = fopen(temp, "r");
219 newFile = fopen(level3File, "w");
220 fseek(fileToCopy, 0, SEEK_END);
221 long length = ftell(fileToCopy);
224 for(long i=0; i<length; i++)
225 fputc(fgetc(fileToCopy), newFile);
233 newFile = fopen(level3File, "w");
234 fprintf(newFile, "---\n");
235 fprintf(newFile, "title: %s\n", title);
237 if (strlen(shortTitle) > 0)
238 fprintf(newFile, "menu_title: %s\n", shortTitle);
240 if (strlen(style) > 0)
241 fprintf(newFile, "style: %s\n", style);
243 fprintf(newFile, "---\n\n");
253 if ((level == 2) && (newFile != NULL))
254 fprintf(newFile, "%s\n", buffer);
258 printf("\n\nProcessed %i lines.\n", lineCount);