]> Shamusworld >> Repos - ardour-manual-diverged/blob - munge.cpp
First stab a major manual restructuring.
[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 //#include <unistd.h>
15
16
17 void MakeFilename(char * fn)
18 {
19         int l = strlen(fn);
20
21         for(int i=0; i<l; i++)
22         {
23                 if (fn[i] == ' ')
24                         fn[i] = '-';
25                 else if (fn[i] >= 'A' && fn[i] <= 'Z')
26                         fn[i] |= 0x20;
27                 else if (fn[i] >= 'a' && fn[i] <= 'z')
28                         ;
29                 else
30                         fn[i] = '_';
31         }
32 }
33
34
35 int main(int argc, char * argv[])
36 {
37         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" };
38
39         FILE * master = fopen("master-doc.txt", "r");
40
41         if (master == NULL)
42         {
43                 printf("Could not open master doc file!\n");
44                 return -1;
45         }
46
47         // Remove contents of _manual before writing into it...
48 //      remove("./_manual/*.html");
49 //      return 0;
50
51         char buffer[1024000], keyword[1024], token[1024];
52         char title[1024], shortTitle[1024], inclFile[1024], style[1024];
53         int level = 0;
54         int lineCount = 0, startLine, sectionLineCount;
55         int part = 0, chapter = 0, subchapter = 0;
56         bool first = true;
57         FILE * newFile = NULL;
58         char level1File[1024], level2File[1024], level3File[1024], temp[1024];
59
60         while (!feof(master))
61         {
62                 fgets(buffer, 1023999, master);
63                 lineCount++;
64
65                 // Look for start of file marker
66                 if (strncmp(buffer, "---", 3) == 0)
67                 {
68                         if (first)
69                                 first = false;
70                         else
71                         {
72                                 sectionLineCount = lineCount - startLine;
73                                 printf(" (%d lines)\n", sectionLineCount);
74                         }
75
76                         // Reset the "short" title
77                         shortTitle[0] = 0;
78                         inclFile[0] = 0;
79                         style[0] = 0;
80
81                         while (true)
82                         {
83                                 fgets(buffer, 1023999, master);
84                                 lineCount++;
85         
86                                 if (strncmp(buffer, "---", 3) != 0)
87                                 {
88                                         sscanf(buffer, "%[^:]: %[^\n]", keyword, token);
89
90                                         if (strcmp(keyword, "title") == 0)
91                                         {
92                                                 strcpy(title, token);
93                                         }
94                                         else if (strcmp(keyword, "part") == 0)
95                                         {
96                                                 int len = strlen(token);
97
98                                                 if (len == 4)
99                                                         level = 0, part++;
100                                                 else if (len == 7)
101                                                         level = 1, chapter++, subchapter = 0;
102                                                 else if (len == 10)
103                                                         level = 2, subchapter++;
104                                                 else
105                                                         level = -1;  // Something went wrong
106                                         }
107                                         else if (strcmp(keyword, "include") == 0)
108                                         {
109                                                 strcpy(inclFile, token);
110                                         }
111                                         else if (strcmp(keyword, "menu_title") == 0)
112                                         {
113                                                 strcpy(shortTitle, token);
114                                         }
115                                         else if (strcmp(keyword, "style") == 0)
116                                         {
117                                                 strcpy(style, token);
118                                         }
119                                         else
120                                                 printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
121                                 }
122                                 else
123                                 {
124                                         // We hit the end of our keyword block, now do something
125                                         // about it... :-P
126 //                                      for(int i=0; i<level; i++)
127 //                                              printf("\t");
128
129                                         if (level == 0)
130                                                 printf("\nPart %s: ", roman[part]);
131                                         else if (level == 1)
132                                                 printf("\n\tCh. %d: ", chapter);
133                                         else if (level == 2)
134                                                 printf("\t\t");
135
136                                         printf("%s", title);
137
138                                         if (strlen(inclFile) > 0)
139                                                 printf(" [%s]", inclFile);
140
141                                         startLine = lineCount;
142
143                                         if (level == 0)
144                                         {
145                                                 // Parts & chapters don't have any content...
146                                                 strcpy(temp, title);
147                                                 MakeFilename(temp);
148                                                 sprintf(level1File, "_manual/%02d_%s", part, temp);
149                                                 mkdir(level1File, 0777);
150                                                 // Make the file expected at this level...
151                                                 sprintf(temp, "%s.html", level1File);
152                                                 FILE * tfp = fopen(temp, "w");
153                                                 fprintf(tfp, "---\n");
154                                                 fprintf(tfp, "title: %s\n", title);
155
156                                                 if (strlen(shortTitle) > 0)
157                                                         fprintf(tfp, "menu_title: %s\n", shortTitle);
158
159                                                 fprintf(tfp, "---\n");
160                                                 fprintf(tfp, "\n{%% children %%}\n\n");
161                                                 fclose(tfp);
162                                         }
163                                         else if (level == 1)
164                                         {
165                                                 strcpy(temp, title);
166                                                 MakeFilename(temp);
167                                                 sprintf(level2File, "%s/%02d_%s", level1File, chapter, temp);
168                                                 mkdir(level2File, 0777);
169                                                 // Make the file expected at this level...
170                                                 sprintf(temp, "%s.html", level2File);
171                                                 FILE * tfp = fopen(temp, "w");
172                                                 fprintf(tfp, "---\n");
173                                                 fprintf(tfp, "title: %s\n", title);
174
175                                                 if (strlen(shortTitle) > 0)
176                                                         fprintf(tfp, "menu_title: %s\n", shortTitle);
177
178                                                 fprintf(tfp, "---\n");
179                                                 fprintf(tfp, "\n{%% children %%}\n\n");
180                                                 fclose(tfp);
181                                         }
182                                         else if (level == 2)
183                                         {
184                                                 strcpy(temp, title);
185                                                 MakeFilename(temp);
186                                                 sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
187
188                                                 // Make the file expected at this level...
189                                                 if (newFile != NULL)
190                                                         fclose(newFile);
191
192                                                 if (strlen(inclFile) > 0)
193                                                 {
194                                                         // Copy the include file to the appropriate spot...
195                                                         sprintf(temp, "include/%s", inclFile);
196                                                         FILE * fileToCopy = fopen(temp, "r");
197                                                         newFile = fopen(level3File, "w");
198                                                         fseek(fileToCopy, 0, SEEK_END);
199                                                         long length = ftell(fileToCopy);
200                                                         rewind(fileToCopy);
201
202                                                         for(long i=0; i<length; i++)
203                                                                 fputc(fgetc(fileToCopy), newFile);
204
205                                                         fclose(newFile);
206                                                         fclose(fileToCopy);
207                                                         newFile = NULL;
208                                                 }
209                                                 else
210                                                 {
211                                                         newFile = fopen(level3File, "w");
212                                                         fprintf(newFile, "---\n");
213                                                         fprintf(newFile, "title: %s\n", title);
214
215                                                         if (strlen(shortTitle) > 0)
216                                                                 fprintf(newFile, "menu_title: %s\n", shortTitle);
217
218                                                         if (strlen(style) > 0)
219                                                                 fprintf(newFile, "style: %s\n", style);
220
221                                                         fprintf(newFile, "---\n\n");
222                                                 }
223                                         }
224
225                                         break;
226                                 }
227                         }
228                 }
229                 else
230                 {
231                         if ((level == 2) && (newFile != NULL))
232                                 fprintf(newFile, "%s\n", buffer);//*/
233                 }
234         }
235
236         printf("\n\nProcessed %i lines.\n", lineCount);
237
238         fclose(master);
239
240         if (newFile)
241                 fclose(newFile);
242
243         return 0;
244 }
245