]> Shamusworld >> Repos - ardour-manual-diverged/blob - munge.cpp
e8c095d10dbc2dea323101dd4ffccc962d497933
[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;
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                                                 int len = strlen(token);
108
109                                                 if (len == 4)
110                                                         level = 0, part++;
111                                                 else if (len == 7)
112                                                         level = 1, chapter++, subchapter = 0;
113                                                 else if (len == 10)
114                                                         level = 2, subchapter++;
115                                                 else
116                                                         level = -1;  // Something went wrong
117                                         }
118                                         else if (strcmp(keyword, "include") == 0)
119                                         {
120                                                 strcpy(inclFile, token);
121                                         }
122                                         else if (strcmp(keyword, "menu_title") == 0)
123                                         {
124                                                 strcpy(shortTitle, token);
125                                         }
126                                         else if (strcmp(keyword, "style") == 0)
127                                         {
128                                                 strcpy(style, token);
129                                         }
130                                         else
131                                                 printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
132                                 }
133                                 else
134                                 {
135                                         // We hit the end of our keyword block, now do something
136                                         // about it... :-P
137                                         if (level == 0)
138                                                 printf("\nPart %s: ", roman[part]);
139                                         else if (level == 1)
140                                                 printf("\n\tCh. %d: ", chapter);
141                                         else if (level == 2)
142                                                 printf("\t\t");
143
144                                         printf("%s", title);
145
146                                         if (strlen(inclFile) > 0)
147                                                 printf(" [%s]", inclFile);
148
149                                         startLine = lineCount;
150
151                                         if (level == 0)
152                                         {
153                                                 // Parts & chapters don't have any content...
154                                                 strcpy(temp, title);
155                                                 MakeFilename(temp);
156                                                 sprintf(level1File, "_manual/%02d_%s", part, temp);
157                                                 mkdir(level1File, 0777);
158
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);
162
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);
168
169                                                 if (strlen(shortTitle) > 0)
170                                                         fprintf(tfp, "menu_title: %s\n", shortTitle);
171
172                                                 fprintf(tfp, "---\n");
173                                                 fprintf(tfp, "\n{%% children %%}\n\n");
174                                                 fclose(tfp);
175                                         }
176                                         else if (level == 1)
177                                         {
178                                                 strcpy(temp, title);
179                                                 MakeFilename(temp);
180                                                 sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
181                                                 mkdir(level2File, 0777);
182
183                                                 // Set up the "chapter" part of the TOC link
184                                                 sprintf(chapterFN, "%s", temp);
185                                                 fprintf(toc, "  <p id=chapter>Ch. %d:&nbsp;&nbsp;<a href=\"/%s/%s/\">%s</a></p>\n", chapter, partFN, chapterFN, title);
186
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);
192
193                                                 if (strlen(shortTitle) > 0)
194                                                         fprintf(tfp, "menu_title: %s\n", shortTitle);
195
196                                                 fprintf(tfp, "---\n");
197                                                 fprintf(tfp, "\n{%% children %%}\n\n");
198                                                 fclose(tfp);
199                                         }
200                                         else if (level == 2)
201                                         {
202                                                 strcpy(temp, title);
203                                                 MakeFilename(temp);
204                                                 sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
205
206                                                 // Make the file expected at this level...
207                                                 if (newFile != NULL)
208                                                         fclose(newFile);
209
210                                                 // Make the link
211                                                 sprintf(scLink, "/%s/%s/%s/", partFN, chapterFN, temp);
212                                                 fprintf(toc, "    <a id=subchapter href=\"%s\">%s</a><br />\n", scLink, title);
213
214                                                 if (strlen(inclFile) > 0)
215                                                 {
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);
222                                                         rewind(fileToCopy);
223
224                                                         for(long i=0; i<length; i++)
225                                                                 fputc(fgetc(fileToCopy), newFile);
226
227                                                         fclose(newFile);
228                                                         fclose(fileToCopy);
229                                                         newFile = NULL;
230                                                 }
231                                                 else
232                                                 {
233                                                         newFile = fopen(level3File, "w");
234                                                         fprintf(newFile, "---\n");
235                                                         fprintf(newFile, "title: %s\n", title);
236
237                                                         if (strlen(shortTitle) > 0)
238                                                                 fprintf(newFile, "menu_title: %s\n", shortTitle);
239
240                                                         if (strlen(style) > 0)
241                                                                 fprintf(newFile, "style: %s\n", style);
242
243                                                         fprintf(newFile, "---\n\n");
244                                                 }
245                                         }
246
247                                         break;
248                                 }
249                         }
250                 }
251                 else
252                 {
253                         if ((level == 2) && (newFile != NULL))
254                                 fprintf(newFile, "%s\n", buffer);
255                 }
256         }
257
258         printf("\n\nProcessed %i lines.\n", lineCount);
259
260         fclose(master);
261         fclose(toc);
262
263         if (newFile)
264                 fclose(newFile);
265
266         return 0;
267 }
268