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>
17 void MakeFilename(char * fn)
21 for(int i=0; i<l; i++)
25 else if (fn[i] >= 'A' && fn[i] <= 'Z')
27 else if (fn[i] >= 'a' && fn[i] <= 'z')
35 int main(int argc, char * argv[])
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" };
39 FILE * master = fopen("master-doc.txt", "r");
43 printf("Could not open master doc file!\n");
47 // Remove contents of _manual before writing into it...
48 // remove("./_manual/*.html");
51 char buffer[1024000], keyword[1024], token[1024];
52 char title[1024], shortTitle[1024], inclFile[1024], style[1024];
54 int lineCount = 0, startLine, sectionLineCount;
55 int part = 0, chapter = 0, subchapter = 0;
57 FILE * newFile = NULL;
58 char level1File[1024], level2File[1024], level3File[1024], temp[1024];
62 fgets(buffer, 1023999, master);
65 // Look for start of file marker
66 if (strncmp(buffer, "---", 3) == 0)
72 sectionLineCount = lineCount - startLine;
73 printf(" (%d lines)\n", sectionLineCount);
76 // Reset the "short" title
83 fgets(buffer, 1023999, master);
86 if (strncmp(buffer, "---", 3) != 0)
88 sscanf(buffer, "%[^:]: %[^\n]", keyword, token);
90 if (strcmp(keyword, "title") == 0)
94 else if (strcmp(keyword, "part") == 0)
96 int len = strlen(token);
101 level = 1, chapter++, subchapter = 0;
103 level = 2, subchapter++;
105 level = -1; // Something went wrong
107 else if (strcmp(keyword, "include") == 0)
109 strcpy(inclFile, token);
111 else if (strcmp(keyword, "menu_title") == 0)
113 strcpy(shortTitle, token);
115 else if (strcmp(keyword, "style") == 0)
117 strcpy(style, token);
120 printf("Unknown keyword '%s' (token: %s)\n", keyword, token);
124 // We hit the end of our keyword block, now do something
126 // for(int i=0; i<level; i++)
130 printf("\nPart %s: ", roman[part]);
132 printf("\n\tCh. %d: ", chapter);
138 if (strlen(inclFile) > 0)
139 printf(" [%s]", inclFile);
141 startLine = lineCount;
145 // Parts & chapters don't have any content...
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);
156 if (strlen(shortTitle) > 0)
157 fprintf(tfp, "menu_title: %s\n", shortTitle);
159 fprintf(tfp, "---\n");
160 fprintf(tfp, "\n{%% children %%}\n\n");
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);
175 if (strlen(shortTitle) > 0)
176 fprintf(tfp, "menu_title: %s\n", shortTitle);
178 fprintf(tfp, "---\n");
179 fprintf(tfp, "\n{%% children %%}\n\n");
186 sprintf(level3File, "%s/%02d_%s.html", level2File, subchapter, temp);
188 // Make the file expected at this level...
192 if (strlen(inclFile) > 0)
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);
202 for(long i=0; i<length; i++)
203 fputc(fgetc(fileToCopy), newFile);
211 newFile = fopen(level3File, "w");
212 fprintf(newFile, "---\n");
213 fprintf(newFile, "title: %s\n", title);
215 if (strlen(shortTitle) > 0)
216 fprintf(newFile, "menu_title: %s\n", shortTitle);
218 if (strlen(style) > 0)
219 fprintf(newFile, "style: %s\n", style);
221 fprintf(newFile, "---\n\n");
231 if ((level == 2) && (newFile != NULL))
232 fprintf(newFile, "%s\n", buffer);//*/
236 printf("\n\nProcessed %i lines.\n", lineCount);