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];
63 int level = 0, lastLevel = -1;
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)
108 int len = strlen(token);
113 level = 1, chapter++, subchapter = 0;
115 level = 2, subchapter++;
117 level = -1; // Something went wrong
119 else if (strcmp(keyword, "include") == 0)
121 strcpy(inclFile, token);
123 else if (strcmp(keyword, "menu_title") == 0)
125 strcpy(shortTitle, token);
127 else if (strcmp(keyword, "style") == 0)
129 strcpy(style, token);
132 printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
136 // We hit the end of our keyword block, now do something
139 printf("\nPart %s: ", roman[part]);
141 printf("\n\tCh. %d: ", chapter);
147 if (strlen(inclFile) > 0)
148 printf(" [%s]", inclFile);
150 startLine = lineCount;
152 // Cleanup our chapter content
157 fprintf(newFile, "\n{%% children %%}\n\n");
165 // Parts & chapters don't have any content...
168 sprintf(level1File, "_manual/%02d_%s", part, temp);
169 mkdir(level1File, 0777);
171 // Set up the "part" level of TOC link
172 sprintf(partFN, "%s", temp);
173 fprintf(toc, "<h2>Part %s: %s</h2>\n", roman[part], title);
175 // Make the file expected at this level...
176 sprintf(temp, "%s.html", level1File);
177 FILE * tfp = fopen(temp, "w");
178 fprintf(tfp, "---\n");
179 fprintf(tfp, "title: %s\n", title);
181 if (strlen(shortTitle) > 0)
182 fprintf(tfp, "menu_title: %s\n", shortTitle);
184 fprintf(tfp, "---\n");
185 fprintf(tfp, "\n{%% children %%}\n\n");
192 sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
193 mkdir(level2File, 0777);
195 // Set up the "chapter" part of the TOC link
196 sprintf(chapterFN, "%s", temp);
197 fprintf(toc, " <p id=chapter>Ch. %d: <a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
199 // Make the file expected at this level...
200 sprintf(temp, "%s.html", level2File);
201 newFile = fopen(temp, "w");
202 fprintf(newFile, "---\n");
203 fprintf(newFile, "title: %s\n", title);
205 if (strlen(shortTitle) > 0)
206 fprintf(newFile, "menu_title: %s\n", shortTitle);
208 fprintf(newFile, "---\n\n");
209 // fprintf(tfp, "\n{%% children %%}\n\n");
216 sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
218 // Make the file expected at this level...
223 sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
224 fprintf(toc, " <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
226 if (strlen(inclFile) > 0)
228 // Copy the include file to the appropriate spot...
229 sprintf(temp, "include/%s", inclFile);
230 FILE * fileToCopy = fopen(temp, "r");
231 newFile = fopen(level3File, "w");
232 fseek(fileToCopy, 0, SEEK_END);
233 long length = ftell(fileToCopy);
236 for(long i=0; i<length; i++)
237 fputc(fgetc(fileToCopy), newFile);
245 newFile = fopen(level3File, "w");
246 fprintf(newFile, "---\n");
247 fprintf(newFile, "title: %s\n", title);
249 if (strlen(shortTitle) > 0)
250 fprintf(newFile, "menu_title: %s\n", shortTitle);
252 if (strlen(style) > 0)
253 fprintf(newFile, "style: %s\n", style);
255 fprintf(newFile, "---\n\n");
265 if (((level == 1) || (level == 2)) && (newFile != NULL))
266 fprintf(newFile, "%s\n", buffer);
270 printf("\n\nProcessed %i lines.\n", lineCount);