]> Shamusworld >> Repos - ardour-manual-diverged/blob - munge.cpp
493cdbd7cb8f1cf21ec8b77fc01b4f8309040a06
[ardour-manual-diverged] / munge.cpp
1 //
2 // Program to massage the master doc file to spit out a bunch of little files
3 // for the Ruby build script...
4 //
5 // by James Hammons
6 // (C) 2016 Underground Software
7 //
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>           // For mkdir()
13 #include <sys/types.h>
14
15
16 void MakeFilename(char * fn)
17 {
18         int l = strlen(fn);
19
20         for(int i=0; i<l; i++)
21         {
22                 if (fn[i] == ' ')
23                         fn[i] = '-';
24                 else if (fn[i] >= 'A' && fn[i] <= 'Z')
25                         fn[i] |= 0x20;
26                 else if (fn[i] >= 'a' && fn[i] <= 'z')
27                         ;
28                 else if (fn[i] >= '0' && fn[i] <= '9')
29                         ;
30                 else
31                         fn[i] = '_';
32         }
33 }
34
35
36 int main(int argc, char * argv[])
37 {
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" };
39
40         FILE * master = fopen("master-doc.txt", "r");
41
42         if (master == NULL)
43         {
44                 printf("Could not open master doc (master-doc.txt) file!\n");
45                 return -1;
46         }
47
48         FILE * toc = fopen("_manual/00_toc.html", "w");
49
50         if (toc == NULL)
51         {
52                 printf("Could not open TOC file!\n");
53                 fclose(master);
54                 return -1;
55         }
56
57         fprintf(toc, "---\n");
58         fprintf(toc, "title: Ardour Table of Contents\n");
59         fprintf(toc, "---\n\n");
60
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;
66         bool first = true;
67         FILE * newFile = NULL;
68         char level1File[1024], level2File[1024], level3File[1024], temp[1024];
69         char partFN[1024], chapterFN[1024], scLink[4096];
70
71         while (!feof(master))
72         {
73                 fgets(buffer, 1023999, master);
74                 lineCount++;
75
76                 // Look for start of file marker
77                 if (strncmp(buffer, "---", 3) == 0)
78                 {
79                         if (first)
80                                 first = false;
81                         else
82                         {
83                                 sectionLineCount = lineCount - startLine;
84                                 printf(" (%d lines)\n", sectionLineCount);
85                         }
86
87                         // Reset the "short" title, include file & sytle
88                         shortTitle[0] = 0;
89                         inclFile[0] = 0;
90                         style[0] = 0;
91
92                         while (true)
93                         {
94                                 fgets(buffer, 1023999, master);
95                                 lineCount++;
96         
97                                 if (strncmp(buffer, "---", 3) != 0)
98                                 {
99                                         sscanf(buffer, "%[^:]: %[^\n]", keyword, token);
100
101                                         if (strcmp(keyword, "title") == 0)
102                                         {
103                                                 strcpy(title, token);
104                                         }
105                                         else if (strcmp(keyword, "part") == 0)
106                                         {
107                                                 lastLevel = level;
108                                                 int len = strlen(token);
109
110                                                 if (len == 4)
111                                                         level = 0, part++;
112                                                 else if (len == 7)
113                                                         level = 1, chapter++, subchapter = 0;
114                                                 else if (len == 10)
115                                                         level = 2, subchapter++;
116                                                 else
117                                                         level = -1;  // Something went wrong
118                                         }
119                                         else if (strcmp(keyword, "include") == 0)
120                                         {
121                                                 strcpy(inclFile, token);
122                                         }
123                                         else if (strcmp(keyword, "menu_title") == 0)
124                                         {
125                                                 strcpy(shortTitle, token);
126                                         }
127                                         else if (strcmp(keyword, "style") == 0)
128                                         {
129                                                 strcpy(style, token);
130                                         }
131                                         else
132                                                 printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
133                                 }
134                                 else
135                                 {
136                                         // We hit the end of our keyword block, now do something
137                                         // about it... :-P
138                                         if (level == 0)
139                                                 printf("\nPart %s: ", roman[part]);
140                                         else if (level == 1)
141                                                 printf("\n\tCh. %d: ", chapter);
142                                         else if (level == 2)
143                                                 printf("\t\t");
144
145                                         printf("%s", title);
146
147                                         if (strlen(inclFile) > 0)
148                                                 printf(" [%s]", inclFile);
149
150                                         startLine = lineCount;
151
152                                         // Cleanup our chapter content
153                                         if (lastLevel == 1)
154                                         {
155                                                 if (newFile != NULL)
156                                                 {
157                                                         fprintf(newFile, "\n{%% children %%}\n\n");
158                                                         fclose(newFile);
159                                                         newFile = NULL;
160                                                 }
161                                         }
162
163                                         if (level == 0)
164                                         {
165                                                 // Parts & chapters don't have any content...
166                                                 strcpy(temp, title);
167                                                 MakeFilename(temp);
168                                                 sprintf(level1File, "_manual/%02d_%s", part, temp);
169                                                 mkdir(level1File, 0777);
170
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);
174
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);
180
181                                                 if (strlen(shortTitle) > 0)
182                                                         fprintf(tfp, "menu_title: %s\n", shortTitle);
183
184                                                 fprintf(tfp, "---\n");
185                                                 fprintf(tfp, "\n{%% children %%}\n\n");
186                                                 fclose(tfp);
187                                         }
188                                         else if (level == 1)
189                                         {
190                                                 strcpy(temp, title);
191                                                 MakeFilename(temp);
192                                                 sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
193                                                 mkdir(level2File, 0777);
194
195                                                 // Set up the "chapter" part of the TOC link
196                                                 sprintf(chapterFN, "%s", temp);
197                                                 fprintf(toc, "  <p id=chapter>Ch. %d:&nbsp;&nbsp;<a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
198
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);
204
205                                                 if (strlen(shortTitle) > 0)
206                                                         fprintf(newFile, "menu_title: %s\n", shortTitle);
207
208                                                 fprintf(newFile, "---\n\n");
209 //                                              fprintf(tfp, "\n{%% children %%}\n\n");
210 //                                              fclose(tfp);
211                                         }
212                                         else if (level == 2)
213                                         {
214                                                 strcpy(temp, title);
215                                                 MakeFilename(temp);
216                                                 sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
217
218                                                 // Make the file expected at this level...
219                                                 if (newFile != NULL)
220                                                         fclose(newFile);
221
222                                                 // Make the link
223                                                 sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
224                                                 fprintf(toc, "    <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
225
226                                                 if (strlen(inclFile) > 0)
227                                                 {
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);
234                                                         rewind(fileToCopy);
235
236                                                         for(long i=0; i<length; i++)
237                                                                 fputc(fgetc(fileToCopy), newFile);
238
239                                                         fclose(newFile);
240                                                         fclose(fileToCopy);
241                                                         newFile = NULL;
242                                                 }
243                                                 else
244                                                 {
245                                                         newFile = fopen(level3File, "w");
246                                                         fprintf(newFile, "---\n");
247                                                         fprintf(newFile, "title: %s\n", title);
248
249                                                         if (strlen(shortTitle) > 0)
250                                                                 fprintf(newFile, "menu_title: %s\n", shortTitle);
251
252                                                         if (strlen(style) > 0)
253                                                                 fprintf(newFile, "style: %s\n", style);
254
255                                                         fprintf(newFile, "---\n\n");
256                                                 }
257                                         }
258
259                                         break;
260                                 }
261                         }
262                 }
263                 else
264                 {
265                         if (((level == 1) || (level == 2)) && (newFile != NULL))
266                                 fprintf(newFile, "%s\n", buffer);
267                 }
268         }
269
270         printf("\n\nProcessed %i lines.\n", lineCount);
271
272         fclose(master);
273         fclose(toc);
274
275         if (newFile)
276                 fclose(newFile);
277
278         return 0;
279 }
280