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
158 fprintf(newFile, "\n{%% children %%}\n\n");
167 // Parts & chapters don't have any content...
170 sprintf(level1File, "_manual/%02d_%s", part, temp);
171 mkdir(level1File, 0777);
173 // Set up the "part" level of TOC link
174 sprintf(partFN, "%s", temp);
175 fprintf(toc, "<h2>Part %s: %s</h2>\n", roman[part], title);
177 // Make the file expected at this level...
178 sprintf(temp, "%s.html", level1File);
179 FILE * tfp = fopen(temp, "w");
180 fprintf(tfp, "---\n");
181 fprintf(tfp, "title: %s\n", title);
183 if (strlen(shortTitle) > 0)
184 fprintf(tfp, "menu_title: %s\n", shortTitle);
186 fprintf(tfp, "---\n");
187 fprintf(tfp, "\n{%% children %%}\n\n");
194 sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
195 mkdir(level2File, 0777);
197 // Set up the "chapter" part of the TOC link
198 sprintf(chapterFN, "%s", temp);
199 fprintf(toc, " <p id=chapter>Ch. %d: <a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
201 // Make the file expected at this level...
202 sprintf(temp, "%s.html", level2File);
203 newFile = fopen(temp, "w");
204 fprintf(newFile, "---\n");
205 fprintf(newFile, "title: %s\n", title);
207 if (strlen(shortTitle) > 0)
208 fprintf(newFile, "menu_title: %s\n", shortTitle);
210 fprintf(newFile, "---\n\n");
211 // fprintf(tfp, "\n{%% children %%}\n\n");
218 sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
220 // Make the file expected at this level...
225 sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
226 fprintf(toc, " <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
228 if (strlen(inclFile) > 0)
230 // Copy the include file to the appropriate spot...
231 sprintf(temp, "include/%s", inclFile);
232 FILE * fileToCopy = fopen(temp, "r");
233 newFile = fopen(level3File, "w");
234 fseek(fileToCopy, 0, SEEK_END);
235 long length = ftell(fileToCopy);
238 for(long i=0; i<length; i++)
239 fputc(fgetc(fileToCopy), newFile);
247 newFile = fopen(level3File, "w");
248 fprintf(newFile, "---\n");
249 fprintf(newFile, "title: %s\n", title);
251 if (strlen(shortTitle) > 0)
252 fprintf(newFile, "menu_title: %s\n", shortTitle);
254 if (strlen(style) > 0)
255 fprintf(newFile, "style: %s\n", style);
257 fprintf(newFile, "---\n\n");
267 if (((level == 1) || (level == 2)) && (newFile != NULL))
268 fprintf(newFile, "%s\n", buffer);
272 printf("\n\nProcessed %i lines.\n", lineCount);