Debian Codegraph @

tools/madoc/madoc.c

raw symbols SCIP
1 /*
2 This is very rough and built specifically for miniaudio. Don't get clever and try using this for
3 your own project because it will most likely not work.
4 */
5
6 #include "../../../fs/fs.c"
7
8 #define C89STR_IMPLEMENTATION
9 #include "../../../c89str/c89str.h"
10
11 /*
12 This runs in two phases. The first phase generates a webplate-compatible template site. The second phase
13 uses webplate to actually generate the actual website.
14 */
15 #include "../../../webplate/source/libwebplate.c"
16
17 /* The output directory, relative to the tools/build/_bin directory. */
18 #define WEBSITE_DOCS_FOLDER "website/docs"
19 #define WEBSITE_DOCS_MANUAL_FOLDER WEBSITE_DOCS_FOLDER"/manual"
20 #define WEBSITE_DOCS_EXAMPLES_FOLDER WEBSITE_DOCS_FOLDER"/examples"
21 #define WEBSITE_DOCS_API_FOLDER WEBSITE_DOCS_FOLDER"/api"
22
23 #define EXAMPLES_FOLDER "examples"
24
25
26
27 c89str load_file(const char* pFilePath)
28 {
29 fs_result resultFS;
30 size_t fileSize;
31 char* pFileData;
32 c89str str;
33
34 resultFS = fs_file_open_and_read(NULL, pFilePath, FS_FORMAT_TEXT, (void**)&pFileData, &fileSize);
35 if (resultFS != FS_SUCCESS) {
36 printf("Failed to open %s: %s.\n", pFilePath, fs_result_description(resultFS));
37 return NULL;
38 }
39
40 /* We have the file data, but now we need to convert it to a dynamic string so we can manipulate it later. */
41 str = c89str_newn(NULL, pFileData, fileSize);
42 fs_free(pFileData, NULL);
43
44 return str;
45 }
46
47 int save_file(const char* pFilePath, c89str src)
48 {
49 fs_result resultFS;
50
51 resultFS = fs_file_open_and_write(NULL, pFilePath, src, c89str_len(src));
52 if (resultFS != FS_SUCCESS) {
53 printf("Failed to save %s: %s.\n", pFilePath, fs_result_description(resultFS));
54 return (int)resultFS;
55 }
56
57 return 0;
58 }
59
60
61 c89str convert_tabs_to_spaces(c89str str)
62 {
63 /* Assume a tab is 4 spaces. */
64 return c89str_replace_all(str, NULL, "\t", 1, " ", 4);
65 }
66
67
68 c89str strip_code_block_comments(c89str str)
69 {
70 size_t runningOffset = 0;
71
72 if (str == NULL) {
73 return str;
74 }
75
76 /*
77 Block comments are not recursive which slightly simplifies our algorithm. The algorithm we're using here is not
78 based on speed and can be made much more efficient. Note that this will stip blocks that contain an opening or
79 closing block within a string constant. The proper way to do this is to use a proper C parser, but that is more
80 than what we need right now.
81 */
82 for (;;) {
83 size_t openingOffset;
84 size_t closingOffset;
85
86 /* Opening. */
87 if (c89str_find(str + runningOffset, "/*", &openingOffset) != C89STR_SUCCESS) {
88 break; /* We're done. */
89 }
90 openingOffset += runningOffset; /* Normalize the opening offset. */
91
92 /* Closing. */
93 if (c89str_find(str + openingOffset, "*/", &closingOffset) != C89STR_SUCCESS) {
94 break; /* We're done. */
95 }
96 closingOffset += openingOffset; /* Normalize the closing offset. */
97 closingOffset += 2; /* Make sure to include the closing block itself. */
98
99 /* We now have enough information to delete the comment block. */
100 str = c89str_replace(str, NULL, openingOffset, (closingOffset - openingOffset), "", 0);
101 if (str == NULL) {
102 return str;
103 }
104
105 /* We're done with this block so we can now move on to the next. */
106 runningOffset = openingOffset;
107 }
108
109 return str;
110 }
111
112 c89str strip_code_line_comments(c89str str)
113 {
114 if (str == NULL) {
115 return str;
116 }
117
118 /* TODO: Implement me. */
119 return str;
120 }
121
122 c89str strip_code_comments(c89str str)
123 {
124 return strip_code_line_comments(strip_code_block_comments(str));
125 }
126
127
128
129 c89str strip_empty_lines(c89str str)
130 {
131 const char* pRunningStr;
132 c89str newstr = NULL;
133 size_t lineLen;
134 size_t nextLineOffset;
135
136 if (str == NULL) {
137 return str;
138 }
139
140 pRunningStr = str;
141
142 /* We are going to fully recreate the string line by line. We can allocate enough space from the start. */
143 newstr = c89str_new_with_cap(NULL, c89str_len(str));
144 if (newstr == NULL) {
145 return str;
146 }
147
148 while (pRunningStr[0] != '\0') {
149 nextLineOffset = c89str_utf8_find_next_line(pRunningStr, (size_t)-1, &lineLen);
150
151 if (c89str_is_null_or_whitespace(pRunningStr, lineLen) == C89STR_FALSE) {
152 newstr = c89str_catn(newstr, NULL, pRunningStr, nextLineOffset);
153 }
154
155 if (nextLineOffset == c89str_npos) {
156 break;
157 } else {
158 pRunningStr += nextLineOffset;
159 }
160 }
161
162 if (newstr == NULL) {
163 return str;
164 }
165
166 c89str_delete(str, NULL);
167 return newstr;
168 }
169
170 c89str strip_trailing_whitespace(c89str str)
171 {
172 const char* pRunningStr;
173 c89str newstr = NULL;
174
175 if (str == NULL) {
176 return str;
177 }
178
179 pRunningStr = str;
180
181 /* We are going to fully recreate the string line by line. We can allocate enough space from the start. */
182 newstr = c89str_new_with_cap(NULL, c89str_len(str));
183 if (newstr == NULL) {
184 return str;
185 }
186
187 while (pRunningStr[0] != '\0') {
188 size_t lineLen;
189 size_t nextLineOffset;
190
191 nextLineOffset = c89str_utf8_find_next_line(pRunningStr, (size_t)-1, &lineLen);
192 newstr = c89str_catn(newstr, NULL, pRunningStr, c89str_utf8_rtrim_offset(pRunningStr, lineLen));
193
194 /* Insert the new line character. */
195 if (nextLineOffset != c89str_npos) {
196 newstr = c89str_catn(newstr, NULL, pRunningStr + lineLen, nextLineOffset - lineLen);
197 } else {
198 newstr = c89str_catn(newstr, NULL, pRunningStr + lineLen, lineLen);
199 }
200
201 if (nextLineOffset == c89str_npos) {
202 break;
203 } else {
204 pRunningStr += nextLineOffset;
205 }
206 }
207
208 if (newstr == NULL) {
209 return str;
210 }
211
212 c89str_delete(str, NULL);
213 return newstr;
214 }
215
216 c89str strip_whitespace(c89str str)
217 {
218 return strip_trailing_whitespace(strip_empty_lines(str));
219 }
220
221 c89str minify_code(c89str str)
222 {
223 return convert_tabs_to_spaces(strip_whitespace(strip_code_comments(str)));
224 }
225
226
227
228
229 static int path_remove_extension(char* dst, size_t dstSizeInBytes, const char* src)
230 {
231 const char* ext;
232
233 if (src == NULL) {
234 if (dst != NULL && dstSizeInBytes > 0) {
235 dst[0] = '\0';
236 }
237
238 return -1;
239 }
240
241 ext = fs_path_extension(src, FS_NULL_TERMINATED);
242 if (ext == NULL || ext[0] == '\0') {
243 /* No extension. */
244 if (dst == src) {
245 return (ext - dst);
246 } else {
247 size_t len = strlen(src);
248 fs_strncpy_s(dst, dstSizeInBytes, src, len);
249
250 return (int)len;
251 }
252 } else {
253 /* Have extension. */
254 size_t dstLen = (size_t)(ext - src - 1); /* -1 for the period. */
255
256 if (dst == src) {
257 dst[dstLen] = '\0';
258 } else {
259 fs_strncpy_s(dst, dstSizeInBytes, src, dstLen);
260 }
261
262 return dstLen;
263 }
264 }
265
266 static int path_append_extension(char* dst, size_t dstSizeInBytes, const char* base, const char* extension)
267 {
268 fs_result result = FS_SUCCESS;
269 size_t baseLength;
270 size_t extLength;
271 size_t dstLen;
272
273 if (base == NULL) {
274 base = "";
275 }
276
277 if (extension == NULL) {
278 extension = "";
279 }
280
281 if (extension[0] == '\0') {
282 if (dst != NULL) {
283 if (dst != base) {
284 fs_strcpy_s(dst, dstSizeInBytes, base);
285 }
286 }
287
288 return (int)strlen(base);
289 }
290
291
292 baseLength = strlen(base);
293 extLength = strlen(extension);
294 dstLen = baseLength + 1 + extLength;
295
296 if (dst != NULL) {
297 if (dstLen+1 <= dstSizeInBytes) {
298 if (dst != base) {
299 fs_strcpy_s(dst + 0, dstSizeInBytes - 0, base);
300 }
301 fs_strcpy_s(dst + baseLength, dstSizeInBytes - baseLength, ".");
302 fs_strcpy_s(dst + baseLength + 1, dstSizeInBytes - baseLength - 1, extension);
303 }
304 }
305
306 return (int)dstLen;
307 }
308
309
310 static fs_result fs_rmdir_content(const char* pDirectory)
311 {
312 /* We'll use an iterator for this. */
313 fs_result result;
314 fs_iterator* pIterator;
315
316 if (pDirectory == NULL) {
317 return FS_INVALID_ARGS;
318 }
319
320 pIterator = fs_first(NULL, pDirectory, FS_READ);
321 while (pIterator != NULL) {
322 char* pFilePath;
323 int filePathLen;
324
325 /* Get the length first. */
326 filePathLen = fs_path_append(NULL, 0, pDirectory, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
327 if (filePathLen > 0) {
328 pFilePath = (char*)fs_malloc((size_t)filePathLen + 1, NULL); /* +1 for null terminator. */
329 if (pFilePath != NULL) {
330 fs_path_append(pFilePath, (size_t)filePathLen + 1, pDirectory, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
331
332 if (pIterator->info.directory) {
333 if (pIterator->pName[0] == '.' && pIterator->pName[1] == '\0') {
334 /* "." - ignore. */
335 } else if (pIterator->pName[0] == '.' && pIterator->pName[1] == '.' && pIterator->pName[2] == '\0') {
336 /* ".." - ignore. */
337 } else {
338 fs_rmdir_content(pFilePath);
339
340 }
341 }
342
343 fs_remove(NULL, pFilePath, 0);
344 } else {
345 return FS_OUT_OF_MEMORY;
346 }
347 } else {
348 /* Failed to retrieve the file path length. */
349 }
350
351 pIterator = fs_next(pIterator);
352 }
353
354 fs_free_iterator(pIterator);
355
356 return FS_SUCCESS;
357 }
358
359
360
361 typedef struct
362 {
363 c89str name;
364 c89str code;
365 } doc_example;
366
367 typedef enum
368 {
369 doc_category_home,
370 doc_category_manual,
371 doc_category_examples,
372 doc_category_api
373 } doc_category;
374
375 typedef enum
376 {
377 doc_token_type_paragraph,
378 doc_token_type_code,
379 doc_token_type_table,
380 doc_token_type_header,
381 doc_token_type_list_item
382 } doc_token_type;
383
384 typedef enum
385 {
386 doc_lang_none,
387 doc_lang_c
388 } doc_lang;
389
390 typedef struct
391 {
392 const char* pText;
393 size_t textLen; /* Set to (size_t)-1 for null terminated. */
394 size_t textOff; /* The cursor. */
395 doc_token_type token;
396 const char* pTokenStr;
397 size_t tokenLen;
398 size_t headerLevel;
399 size_t indentation; /* Useful for knowing how to offset code. Code tags can be indented and we want to know how deep the base level of indentation is. */
400 size_t prevLineOff;
401 size_t prevLineLen;
402 doc_lang codeLang;
403 int isLastListItem;
404 size_t listItemCounter;
405 } doc_lexer;
406
407 int doc_lexer_init(const char* pText, size_t textLen, doc_lexer* pLexer)
408 {
409 if (pText == NULL || pLexer == NULL) {
410 return EINVAL;
411 }
412
413 C89STR_ZERO_OBJECT(pLexer);
414
415 if (textLen == (size_t)-1) {
416 textLen = c89str_strlen(pText);
417 }
418
419 pLexer->pText = pText;
420 pLexer->textLen = textLen;
421 pLexer->textOff = 0;
422
423 return 0;
424 }
425
426 static int doc_lexer_is_header_underline(const char* pText, size_t textLen, char ch)
427 {
428 size_t i;
429
430 if (textLen == 0) {
431 return 0;
432 }
433
434 for (i = 0; i < textLen; i += 1) {
435 if (pText[i] == '\0') {
436 break;
437 }
438
439 if (pText[i] != ch) {
440 return 0;
441 }
442 }
443
444 /* Getting here means it's an underline. */
445 return 1;
446 }
447
448 static size_t doc_lexer_get_header_underline_level(const char* pText, size_t textLen)
449 {
450 if (doc_lexer_is_header_underline(pText, textLen, '-')) {
451 return 2;
452 }
453
454 if (doc_lexer_is_header_underline(pText, textLen, '=')) {
455 return 1;
456 }
457
458 return 0;
459 }
460
461 int doc_lexer_next(doc_lexer* pLexer)
462 {
463 //int result;
464 const char* txt;
465 size_t off;
466 size_t len;
467
468 if (pLexer == NULL) {
469 return EINVAL;
470 }
471
472 txt = pLexer->pText;
473 off = pLexer->textOff; /* Moves forward. */
474 len = pLexer->textLen; /* Constant. */
475
476 /* We run line-by-line. */
477 for (;;) {
478 size_t thisLineLen;
479 size_t nextLineBeg;
480 size_t indentation;
481 size_t headerLevel;
482 size_t location; /* When searching for a token on a line. */
483
484 if (txt[off] == '\0' || len == off) {
485 /* We're done. The last paragraph needs to be added, if any. */
486 if (off > pLexer->textOff) {
487 pLexer->token = doc_token_type_paragraph;
488 pLexer->pTokenStr = txt + pLexer->textOff;
489 pLexer->tokenLen = off - pLexer->textOff;
490 pLexer->textOff = off;
491 return 0;
492 } else {
493 return ENOMEM; /* We're done. */
494 }
495 }
496
497 /* Grab the next line. */
498 nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
499
500 /* Check if we have a header underline. If so, we want to end the paragraph, if any, and then return. */
501 headerLevel = doc_lexer_get_header_underline_level(txt + off, thisLineLen);
502 if (headerLevel > 0) {
503 if (pLexer->textOff < pLexer->prevLineOff) {
504 /* We have a paragraph to post. */
505 pLexer->token = doc_token_type_paragraph;
506 pLexer->pTokenStr = txt + pLexer->textOff;
507 pLexer->tokenLen = pLexer->prevLineOff - pLexer->textOff;
508 pLexer->textOff = off;
509 } else {
510 /* We don't have a paragraph. Just post the header itself. */
511 pLexer->token = doc_token_type_header;
512 pLexer->pTokenStr = txt + pLexer->prevLineOff;
513 pLexer->tokenLen = pLexer->prevLineLen;
514 pLexer->textOff = off + nextLineBeg;
515 pLexer->headerLevel = headerLevel;
516 }
517
518 return 0;
519 }
520
521 /* Getting here means it's not a header. */
522 indentation = c89str_utf8_ltrim_offset(txt + off, len - off);
523
524 if (c89str_findn(txt + off, thisLineLen, "```", (size_t)-1, &location) == C89STR_SUCCESS) {
525 /* The beginning of a code block. If there's a pending paragraph that needs to be posted. */
526 if (pLexer->textOff < off) {
527 /* We have a paragraph to post. */
528 pLexer->token = doc_token_type_paragraph;
529 pLexer->pTokenStr = txt + pLexer->textOff;
530 pLexer->tokenLen = off - pLexer->textOff;
531 pLexer->textOff = off;
532 } else {
533 /* We don't have a paragraph. We can now parse the code block. */
534 size_t tokenOff = off;
535 size_t tokenLen = 0;
536 doc_lang lang = doc_lang_none;
537 if (txt[off + location + 3] == 'c') {
538 lang = doc_lang_c;
539 }
540
541 nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
542 tokenOff = off + nextLineBeg;
543 tokenLen = 0;
544
545 off += nextLineBeg;
546
547 /* We now need to keep looping over each line until we find the closing the code block. */
548 for (;;) {
549 if (txt[off] == '\0' || off == len) {
550 break;
551 }
552
553 nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
554
555 if (c89str_findn(txt + off, thisLineLen, "```", (size_t)-1, &location) == FS_SUCCESS) {
556 /* We found the end of the code block. */
557 pLexer->token = doc_token_type_code;
558 pLexer->pTokenStr = txt + tokenOff;
559 pLexer->tokenLen = tokenLen;
560 pLexer->textOff = off + nextLineBeg;
561 pLexer->indentation = indentation;
562 pLexer->codeLang = lang;
563 break;
564 } else {
565
566 }
567
568 off += nextLineBeg;
569 tokenLen += nextLineBeg;
570 }
571 }
572
573 return 0;
574 }
575
576 /* Getting here means it's not a header nor a code block. It might be a table. */
577 if (c89str_findn(txt + off, thisLineLen, "+--", (size_t)-1, &location) == FS_SUCCESS) {
578 /* The beginning of a table. The end of the table is the end of the last line that starts with a "+" or "|". */
579 if (pLexer->textOff < off) {
580 /* We have a paragraph to post. */
581 pLexer->token = doc_token_type_paragraph;
582 pLexer->pTokenStr = txt + pLexer->textOff;
583 pLexer->tokenLen = off - pLexer->textOff;
584 pLexer->textOff = off;
585 } else {
586 /* We don't have a paragraph. We can now parse the table block. */
587 size_t tokenOff = off;
588 size_t tokenLen = 0;
589
590 for (;;) {
591 if (txt[off] == '\0' || off == len) {
592 break;
593 }
594
595 nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
596
597 if (nextLineBeg < 4 || (txt[off + indentation] != '+' && txt[off + indentation] != '|')) {
598 /* We found the end of the table. The end of the table is the end of this line. */
599 pLexer->token = doc_token_type_table;
600 pLexer->pTokenStr = txt + tokenOff;
601 pLexer->tokenLen = tokenLen;
602 pLexer->textOff = off + nextLineBeg;
603 pLexer->indentation = indentation;
604 break;
605 } else {
606 /* We're still looking at the table. */
607 }
608
609 off += nextLineBeg;
610 tokenLen += nextLineBeg;
611 }
612 }
613
614 return 0;
615 }
616
617
618 /* Check if it's a bullet point. */
619 if (txt[off + indentation] == '-' || txt[off + indentation] == '*') {
620 if (pLexer->textOff < off) {
621 /* There's a pending paragraph to post. */
622 pLexer->token = doc_token_type_paragraph;
623 pLexer->pTokenStr = txt + pLexer->textOff;
624 pLexer->tokenLen = off - pLexer->textOff;
625 pLexer->textOff = off;
626 } else {
627 /* A bullet point can cover multiple lines. The bullet point ends when a line starts with another bullet point or is empty. */
628 size_t tokenOff = off + indentation + 2;
629 size_t tokenLen = 0;
630 //int isLineBlank = 0;
631 int isNextLineBlank = 0;
632
633 off += indentation + 2;
634
635 /* Keep going until we find the start of a new bullet point or an empty line. */
636 for (;;) {
637 int foundEndOfBulletPoint = 0;
638
639 if (txt[off] == '\0' || off == len) {
640 break; /* At end. */
641 }
642
643 /* Go to the next line. */
644 nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
645
646 /* Determine whether or not the next line is blank. */
647 isNextLineBlank = c89str_is_null_or_whitespace(txt + off + nextLineBeg, c89str_utf8_find_next_line(txt + off + nextLineBeg, len - off - nextLineBeg, NULL));
648 if (isNextLineBlank) {
649 foundEndOfBulletPoint = 1;
650 } else {
651 /* The next line is not blank so now check if it starts with a bullet point. */
652 size_t lineContentOff = off + nextLineBeg + c89str_utf8_ltrim_offset(txt + off + nextLineBeg, len - off - nextLineBeg);
653 if (txt[lineContentOff] == '-' || txt[lineContentOff] == '*' || txt[lineContentOff] == '\0') {
654 foundEndOfBulletPoint = 1;
655 }
656 }
657
658 if (foundEndOfBulletPoint) {
659 /* We found the end of the bullet point. Update the lexer and return. */
660 pLexer->token = doc_token_type_list_item;
661 pLexer->pTokenStr = txt + tokenOff;
662 pLexer->tokenLen = tokenLen + thisLineLen;
663 pLexer->textOff = off + nextLineBeg;
664 pLexer->indentation = indentation;
665
666 /* Reset the list item counter if necessary. */
667 if (pLexer->isLastListItem) {
668 pLexer->listItemCounter = 0;
669 }
670
671 pLexer->listItemCounter += 1;
672 pLexer->isLastListItem = isNextLineBlank;
673
674 return 0;
675 } else {
676 /* We didn't find the end of the bullet point. Move forward. */
677 off += indentation + nextLineBeg;
678 tokenLen += indentation + nextLineBeg;
679 }
680 }
681 }
682
683 return 0;
684 }
685
686
687 /* Not a header underline, code block or table. Assume a paragraph. If we have an empty line it means we can terminate our paragraph. */
688 if (c89str_is_null_or_whitespace(txt + off, thisLineLen)) {
689 if (pLexer->textOff < off) {
690 /* We have a paragraph to post. */
691 pLexer->token = doc_token_type_paragraph;
692 pLexer->pTokenStr = txt + pLexer->textOff;
693 pLexer->tokenLen = off - pLexer->textOff;
694 pLexer->textOff = off;
695
696 if (!c89str_is_null_or_whitespace(pLexer->pTokenStr, pLexer->tokenLen)) {
697 return 0;
698 }
699 }
700 }
701
702
703 /* We need to keep track of the previous line for the purpose of header underlines. */
704 pLexer->prevLineOff = off;
705 pLexer->prevLineLen = thisLineLen;
706
707 /* We're done. We can move to the next line now. */
708 off += nextLineBeg;
709 }
710
711 /*return 0;*/
712 }
713
714
715 typedef struct
716 {
717 c89str title;
718 c89str bookmark;
719 size_t level;
720 } manual_section;
721
722 typedef struct
723 {
724 struct
725 {
726 c89str raw;
727 size_t sectionCount;
728 manual_section sections[256];
729 } manual;
730
731 struct
732 {
733 size_t count;
734 doc_example examples[256];
735 } examples;
736 } docstate;
737
738 c89str extract_manual_section_title(const char* pStr, size_t len, size_t* pLevel)
739 {
740 if (pLevel != NULL) {
741 *pLevel = 0;
742 }
743
744 /* We want to remove the numbers because we have limited horizontal space. */
745 while (pStr[0] != '\0' && len > 0) {
746 if ((pStr[0] >= '0' && pStr[0] <= '9') || pStr[0] == '.') {
747 pStr += 1;
748 len -= 1;
749
750 if (pStr[0] == '.' && pLevel != NULL) {
751 *pLevel += 1;
752 }
753
754 continue;
755 } else {
756 break;
757 }
758 }
759
760 return c89str_trim(c89str_newn(NULL, pStr, len), NULL);
761 }
762
763 c89str extract_manual_section_bookmark(const char* pStr, size_t len)
764 {
765 return c89str_replace_all(extract_manual_section_title(pStr, len, NULL), NULL, " ", 1, "-", 0);
766 }
767
768 int load_manual(c89str miniaudio, docstate* pState)
769 {
770 int result;
771 c89str_lexer lexer;
772 int commentCounter;
773
774 result = c89str_lexer_init(&lexer, miniaudio, c89str_len(miniaudio));
775 if (result != 0) {
776 return result;
777 }
778
779 /* The manual will be in the second comment block. The first comment block is the project summary. */
780 commentCounter = 0;
781 for (;;) {
782 result = c89str_lexer_next(&lexer);
783 if (result != 0) {
784 return result;
785 }
786
787 if (lexer.token == c89str_token_type_comment) {
788 commentCounter += 1;
789 }
790
791 if (commentCounter == 2) {
792 /* We've found the comment block with the content of the programming manual. We just need to remove the enveloping comment tokens. */
793 c89str_lexer_transform_token(&lexer, &pState->manual.raw, NULL);
794 if (pState->manual.raw == NULL) {
795 return ENOMEM;
796 } else {
797 break;
798 }
799 }
800 }
801
802 /* We need to extract all the sections of the manual for the purpose of the navigation panel. */
803 {
804 doc_lexer docLexer;
805
806 result = doc_lexer_init(pState->manual.raw, c89str_len(pState->manual.raw), &docLexer);
807 if (result != 0) {
808 return ENOMEM;
809 }
810
811 for (;;) {
812 result = doc_lexer_next(&docLexer);
813 if (result != 0) {
814 break; /* We're done. */
815 }
816
817 if (docLexer.token == doc_token_type_header) {
818 manual_section section;
819
820 section.title = extract_manual_section_title(docLexer.pTokenStr, docLexer.tokenLen, &section.level);
821 section.bookmark = extract_manual_section_bookmark(docLexer.pTokenStr, docLexer.tokenLen);
822
823 pState->manual.sections[pState->manual.sectionCount] = section;
824 pState->manual.sectionCount += 1;
825 }
826 }
827 }
828
829 return 0;
830 }
831
832 int load_examples(docstate* pState)
833 {
834 fs_result resultFS;
835 fs_iterator* pIterator;
836
837 /* Extract all examples. */
838 pIterator = fs_first(NULL, EXAMPLES_FOLDER, FS_READ);
839 while (pIterator != NULL) {
840 if (pIterator->info.size > 0) { /* Ignore any empty files. Sometimes I'll put placeholder files in the examples folder to keep track of ideas for examples. */
841 /* We need to get the contents of the file. */
842 doc_example example;
843 char* pFileData;
844 char filePath[4096];
845
846 fs_path_append(filePath, sizeof(filePath), EXAMPLES_FOLDER, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
847
848 /* Name. We don't care about the whole file path - just the name part of it. */
849 example.name = c89str_newn(NULL, pIterator->pName, pIterator->nameLen);
850
851 /* File content. */
852 resultFS = fs_file_open_and_read(NULL, filePath, FS_FORMAT_TEXT, (void**)&pFileData, NULL);
853 if (resultFS != FS_SUCCESS) {
854 printf("Failed to load example: %s\n", pIterator->pName);
855 return -1;
856 }
857
858 example.code = c89str_new(NULL, pFileData);
859 fs_free(pFileData, NULL);
860
861 pState->examples.examples[pState->examples.count] = example;
862 pState->examples.count += 1;
863 }
864
865 pIterator = fs_next(pIterator);
866 }
867
868 fs_free_iterator(pIterator);
869
870 return 0;
871 }
872
873 int load_api(c89str miniaudio, docstate* pState)
874 {
875 (void)miniaudio;
876 (void)pState;
877 return 0;
878 }
879
880
881 int load(docstate* pState)
882 {
883 int result;
884 c89str miniaudio;
885
886 C89STR_ZERO_OBJECT(pState);
887
888 /* Data is extracted from miniaudio.h, so we'll need to get that loaded as a start. */
889 miniaudio = load_file("miniaudio.h");
890 if (miniaudio == NULL) {
891 printf("Failed to load miniaudio.h");
892 return -1;
893 }
894
895 result = load_manual(miniaudio, pState);
896 if (result != 0) {
897 printf("Failed to load manual.");
898 return result;
899 }
900
901 result = load_examples(pState);
902 if (result != 0) {
903 printf("Failed to load examples.");
904 return result;
905 }
906
907 result = load_api(miniaudio, pState);
908 if (result != 0) {
909 printf("Failed to load API.");
910 return result;
911 }
912
913 /* We're done. */
914 c89str_delete(miniaudio, NULL);
915 return 0;
916 }
917
918 c89str raw_to_html(c89str raw);
919
920 c89str transform_inline_code(c89str html)
921 {
922 size_t offset = 0;
923
924 /*
925 To do this we just need to find the next grave character. Then the content between that and the following grave needs to be wrapped in a <span></span> tag
926 with a monospace font.
927 */
928 for (;;) {
929 size_t loc1;
930 size_t loc2;
931 c89str inner;
932 c89str replacement;
933
934 if (c89str_find(html + offset, "`", &loc1) != C89STR_SUCCESS) {
935 break; /* No more occurances. */
936 }
937
938 if (c89str_find(html + offset + loc1 + 1, "`", &loc2) != C89STR_SUCCESS) {
939 break; /* No more occurances. */
940 }
941 loc2 += loc1 + 1; /* Make the closing grave offset relative to "offset" like loc1. */
942
943 /* We need a copy of the section inside the graves just in case the replacement operation needs to reallocate the pointer. */
944 inner = c89str_newn(NULL, html + offset + loc1 + 1, loc2 - loc1 - 1);
945 if (inner == NULL) {
946 return NULL; /* Out of memory. */
947 }
948
949 replacement = c89str_newf(NULL, "<span style=\"font-family:monospace;\">%s</span>", inner);
950 c89str_delete(inner, NULL);
951
952 if (replacement == NULL) {
953 return NULL;
954 }
955
956 /* We now have what we need to replace the segment. */
957 html = c89str_replace(html, NULL, offset + loc1, loc2 - loc1 + 1, replacement, c89str_len(replacement));
958 if (html == NULL) {
959 c89str_delete(replacement, NULL);
960 return NULL; /* Out of memory. */
961 }
962
963 offset += loc1 + c89str_len(replacement);
964 c89str_delete(replacement, NULL);
965 }
966
967 return html;
968 }
969
970 c89str transform_urls_by_protocol(c89str html, const char* pProtocol)
971 {
972 size_t offset = 0;
973 size_t protocolLen;
974
975 protocolLen = c89str_strlen(pProtocol);
976 if (protocolLen == 0) {
977 return html;
978 }
979
980 for (;;) {
981 size_t loc1;
982 size_t loc2;
983 c89str url;
984 c89str replacement;
985
986 if (c89str_find(html + offset, pProtocol, &loc1) != C89STR_SUCCESS) {
987 break; /* No more occurances. */
988 }
989
990 loc2 = c89str_find_next_whitespace(html + offset + loc1, (size_t)-1, NULL);
991 if (loc2 != c89str_npos) {
992 loc2 += loc1;
993 } else {
994 loc2 = c89str_len(html) - offset;
995 }
996
997 /* We need a copy of the section inside the graves just in case the replacement operation needs to reallocate the pointer. */
998 url = c89str_trim(c89str_newn(NULL, html + offset + loc1, loc2 - loc1), NULL);
999 if (url == NULL) {
1000 return NULL; /* Out of memory. */
1001 }
1002
1003 /* We're going to remove any trailing symbols. */
1004 size_t len = c89str_len(url);
1005 while (url[len-1] == '.' || url[len-1] == ')' || url[len-1] == '(' || url[len-1] == ';') {
1006 len -= 1;
1007 }
1008
1009 url = c89str_setn(url, NULL, url, len); /* Should never fail since we're just shrinking the string. */
1010 loc2 = loc1 + len;
1011
1012 replacement = c89str_newf(NULL, "<a href=\"%s\">%s</a>", url, url);
1013 c89str_delete(url, NULL);
1014
1015 if (replacement == NULL) {
1016 return NULL;
1017 }
1018
1019 /* We now have what we need to replace the segment. */
1020 html = c89str_replace(html, NULL, offset + loc1, loc2 - loc1, replacement, c89str_len(replacement));
1021 if (html == NULL) {
1022 c89str_delete(replacement, NULL);
1023 return NULL; /* Out of memory. */
1024 }
1025
1026 offset += loc1 + c89str_len(replacement);
1027 c89str_delete(replacement, NULL);
1028 }
1029
1030 return html;
1031 }
1032
1033 c89str transform_urls(c89str html)
1034 {
1035 /* This is similar to inline code segments, except out opening token is "http://" or "https://" and ends with */
1036 html = transform_urls_by_protocol(html, "https://");
1037 html = transform_urls_by_protocol(html, "http://");
1038 return html;
1039 }
1040
1041 c89str escape_html(const char* pHTML, size_t len)
1042 {
1043 c89str html = c89str_newn(NULL, pHTML, len);
1044
1045 /* Slow, but it's simple and it works. */
1046 html = c89str_replace_all(html, NULL, "&", (size_t)-1, "&amp;", (size_t)-1); /* <-- This must come first to ensure it doesn't replace the "&" symbols used in the escapes before it. */
1047 html = c89str_replace_all(html, NULL, "<", (size_t)-1, "&lt;", (size_t)-1);
1048 html = c89str_replace_all(html, NULL, ">", (size_t)-1, "&gt;", (size_t)-1);
1049 html = c89str_replace_all(html, NULL, "\"", (size_t)-1, "&quot;", (size_t)-1);
1050 html = c89str_replace_all(html, NULL, "\'", (size_t)-1, "&#39;", (size_t)-1);
1051
1052 /* We want to keep <br> tags unescaped. This is not a good way to do this, but it works well enough for now since we won't in practice have this string in our documentation. */
1053 html = c89str_replace_all(html, NULL, "&lt;br&gt;", (size_t)-1, "<br>", (size_t)-1);
1054
1055 /* Content inside `` tags need to be formatted as code. */
1056 html = transform_inline_code(html);
1057
1058 /* URLs need to be transformed. */
1059 html = transform_urls(html);
1060
1061 return html;
1062
1063 }
1064
1065 c89str raw_to_html_p(const char* pText, size_t textLen)
1066 {
1067 c89str p = NULL;
1068
1069 /* Don't emit anything if the paragraph is empty. */
1070 if (c89str_is_null_or_whitespace(pText, textLen)) {
1071 return c89str_new(NULL, "");
1072 }
1073
1074 p = c89str_cat(p, NULL, "<p>\n");
1075 {
1076 p = c89str_cat(p, NULL, escape_html(pText, textLen));
1077 }
1078 p = c89str_cat(p, NULL, "</p>\n");
1079
1080 return p;
1081 }
1082
1083 const char* g_CKeywords[] = {
1084 "auto",
1085 "break",
1086 "case",
1087 "char",
1088 "const",
1089 "continue",
1090 "default",
1091 "do",
1092 "double",
1093 "else",
1094 "enum",
1095 "extern",
1096 "float",
1097 "for",
1098 "goto",
1099 "if",
1100 "inline",
1101 "int",
1102 "long",
1103 "register",
1104 "restrict",
1105 "return",
1106 "short",
1107 "signed",
1108 "sizeof",
1109 "static",
1110 "struct",
1111 "switch",
1112 "typedef",
1113 "union",
1114 "unsigned",
1115 "void",
1116 "volatile",
1117 "while",
1118 };
1119
1120 /* This is temporary until we get a basic C parser working and we can dynamically extract our custom types. */
1121 const char* g_CustomTypes[] = {
1122 "ma_uint8",
1123 "ma_int8",
1124 "ma_uint16",
1125 "ma_int16",
1126 "ma_uint32",
1127 "ma_int32",
1128 "ma_uint64",
1129 "ma_int64",
1130 "ma_bool8",
1131 "ma_bool32",
1132 "ma_mutex",
1133 "ma_semaphore",
1134 "ma_event",
1135 "ma_thread",
1136 "ma_result",
1137 "ma_context_config",
1138 "ma_context",
1139 "ma_device_config",
1140 "ma_device",
1141 "ma_device_info",
1142 "ma_decoder_config",
1143 "ma_decoder",
1144 "ma_encoder_config",
1145 "ma_encoder",
1146 "ma_waveform_config",
1147 "ma_waveform",
1148 "ma_noise_config",
1149 "ma_noise",
1150 "ma_audio_buffer_config",
1151 "ma_audio_buffer",
1152 "ma_data_source",
1153 "ma_pcm_rb",
1154 "ma_rb",
1155 "ma_channel_converter_config",
1156 "ma_channel_converter",
1157 "ma_resampler_config",
1158 "ma_resampler",
1159 "ma_data_converter_config",
1160 "ma_data_converter",
1161 "ma_biquad_config",
1162 "ma_biquad",
1163 "ma_lpf_config",
1164 "ma_lpf",
1165 };
1166
1167 #define C_COMMENT_COLOR "#009900"
1168 #define C_STRING_COLOR "#cc3300"
1169 #define C_KEYWORD_COLOR "#0033ff"
1170 #define C_PREPROCESSOR_COLOR "#666666"
1171 #define C_CUSTOM_TYPE_COLOR "#0099cc"
1172
1173 c89str_bool32 list_contains_string(const char** ppList, size_t listCount, const char* pText, size_t textLen)
1174 {
1175 size_t i = 0;
1176 for (i = 0; i < listCount; i += 1) {
1177 if (strncmp(ppList[i], pText, textLen) == 0) {
1178 return C89STR_TRUE;
1179 }
1180 }
1181
1182 return C89STR_FALSE;
1183 }
1184
1185 c89str_bool32 is_c_keyword(const char* pText, size_t textLen)
1186 {
1187 return list_contains_string(g_CKeywords, C89STR_COUNTOF(g_CKeywords), pText, textLen);
1188 }
1189
1190 c89str_bool32 is_custom_type(const char* pText, size_t textLen)
1191 {
1192 return list_contains_string(g_CustomTypes, C89STR_COUNTOF(g_CustomTypes), pText, textLen);
1193 }
1194
1195 c89str html_highlight(const char* pText, size_t textLen, const char* pColor)
1196 {
1197 c89str html = NULL;
1198
1199 html = c89str_catf(html, NULL, "<span style=\"color:%s\">", pColor);
1200 html = c89str_cat (html, NULL, escape_html(pText, textLen));
1201 html = c89str_cat (html, NULL, "</span>");
1202
1203 return html;
1204 }
1205
1206 c89str raw_to_html_code(const char* pText, size_t textLen, size_t indentation, doc_lang lang)
1207 {
1208 c89str code = NULL;
1209
1210 if (textLen == (size_t)-1) {
1211 textLen = c89str_strlen(pText);
1212 }
1213
1214 if (lang == doc_lang_none) {
1215 code = c89str_cat(code, NULL, "<div style=\"font-family:monospace; margin:1em 0em;\"><pre style=\"margin:0.5em 1em; padding:0; line-height:125%; overflow-x:auto; overflow-y:hidden;\">\n");
1216 } else {
1217 code = c89str_cat(code, NULL, "<div style=\"font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em; width:100%;\"><pre style=\"margin:0.5em 1em; padding:0; line-height:125%; overflow-x:auto; overflow-y:hidden;\">\n");
1218 }
1219 {
1220 /* We need to do a quick pre-processing of the string to strip the indentation. Then we need to iterate over each token and reconstruct the string. */
1221 int result;
1222 c89str text = NULL;
1223 const char* pRunningText = pText;
1224 c89str_lexer lexer;
1225
1226 while (pRunningText < pText + textLen) {
1227 size_t thisLineLen;
1228 size_t nextLineBeg = c89str_utf8_find_next_line(pRunningText, textLen - (pRunningText - pText), &thisLineLen);
1229
1230 if (nextLineBeg > indentation) {
1231 text = c89str_catn(text, NULL, pRunningText + indentation, nextLineBeg - indentation);
1232 } else {
1233 text = c89str_catn(text, NULL, pRunningText, nextLineBeg);
1234 }
1235
1236 if (nextLineBeg == thisLineLen) {
1237 break;
1238 }
1239
1240 pRunningText += nextLineBeg;
1241 }
1242
1243 /* The code should be stripped of it's indentation so now we need to iterate over each token and construct a html string. */
1244 result = c89str_lexer_init(&lexer, text, c89str_len(text));
1245 if (result == 0) {
1246 while (c89str_lexer_next(&lexer) == 0) {
1247 if (lexer.token == c89str_token_type_comment) {
1248 code = c89str_cat(code, NULL, html_highlight(lexer.pTokenStr, lexer.tokenLen, C_COMMENT_COLOR));
1249 } else if (lexer.token == c89str_token_type_string_double || lexer.token == c89str_token_type_string_single) {
1250 code = c89str_cat(code, NULL, html_highlight(lexer.pTokenStr, lexer.tokenLen, C_STRING_COLOR));
1251 } else {
1252 /* Special case if we're handling a pre-processor keyword. We want to get the next identifier and highlight the entire segment appropriately. */
1253 if (lexer.token == '#') {
1254 const char* pSegmentStr = lexer.pTokenStr;
1255 while (c89str_lexer_next(&lexer) == 0) {
1256 if (lexer.token == c89str_token_type_eof) {
1257 break;
1258 }
1259
1260 if (lexer.token == c89str_token_type_identifier) {
1261 code = c89str_cat(code, NULL, html_highlight(pSegmentStr, (lexer.pTokenStr - pSegmentStr) + lexer.tokenLen, C_PREPROCESSOR_COLOR));
1262
1263 if (strncmp(lexer.pTokenStr, "include", lexer.tokenLen) == 0) {
1264 while (c89str_lexer_next(&lexer) == 0 && lexer.token == c89str_token_type_whitespace) {
1265 code = c89str_cat(code, NULL, escape_html(lexer.pTokenStr, lexer.tokenLen));
1266 }
1267
1268 /* If the token is "<" we will want to make sure we highlight it as a string. */
1269 if (lexer.token == c89str_token_type_string_double || lexer.token == c89str_token_type_string_single) {
1270 code = c89str_cat(code, NULL, html_highlight(lexer.pTokenStr, lexer.tokenLen, C_STRING_COLOR));
1271 } else {
1272 if (lexer.token == '<') {
1273 /* We're highligting an #include. */
1274 const char* pIncludeStr = lexer.pTokenStr;
1275 while (c89str_lexer_next(&lexer) == 0 && lexer.token != '>') {
1276 if (lexer.token == c89str_token_type_eof) {
1277 break;
1278 }
1279 }
1280
1281 code = c89str_cat(code, NULL, html_highlight(pIncludeStr, (lexer.pTokenStr - pIncludeStr) + lexer.tokenLen, C_STRING_COLOR));
1282 }
1283 }
1284 }
1285
1286 break;
1287 }
1288 }
1289 } else {
1290 if (lang == doc_lang_c) {
1291 if (is_c_keyword(lexer.pTokenStr, lexer.tokenLen)) {
1292 code = c89str_cat(code, NULL, html_highlight(lexer.pTokenStr, lexer.tokenLen, C_KEYWORD_COLOR));
1293 } else if (is_custom_type(lexer.pTokenStr, lexer.tokenLen)) {
1294 code = c89str_cat(code, NULL, html_highlight(lexer.pTokenStr, lexer.tokenLen, C_CUSTOM_TYPE_COLOR));
1295 } else {
1296 code = c89str_cat(code, NULL, escape_html(lexer.pTokenStr, lexer.tokenLen));
1297 }
1298 } else {
1299 code = c89str_cat(code, NULL, escape_html(lexer.pTokenStr, lexer.tokenLen));
1300 }
1301 }
1302 }
1303 }
1304 }
1305 }
1306 code = c89str_cat(code, NULL, "</pre></div>");
1307
1308 return code;
1309 }
1310
1311
1312 /* We never have huge tables in our documentation. */
1313 #define MAX_TABLE_COLUMNS 16
1314 #define MAX_TABLE_ROWS 128
1315
1316 typedef struct
1317 {
1318 size_t cellCount;
1319 c89str cells[MAX_TABLE_COLUMNS];
1320 } table_row;
1321
1322 typedef struct
1323 {
1324 size_t colCount;
1325 size_t rowCount;
1326 table_row rows[MAX_TABLE_ROWS]; /* First row is the header. */
1327 } table;
1328
1329 void merge_table_row(table_row* pTarget, const table_row* pSource)
1330 {
1331 size_t cellCount;
1332 size_t iCell;
1333
1334 /* This assumes the cell count is the same. */
1335 cellCount = pTarget->cellCount;
1336
1337 for (iCell = 0; iCell < cellCount; iCell += 1) {
1338 pTarget->cells[iCell] = c89str_catf(pTarget->cells[iCell], NULL, "\n%s", pSource->cells[iCell]);
1339 }
1340 }
1341
1342 table_row parse_table_row(c89str line)
1343 {
1344 table_row row;
1345 const char* pRunningStr = line;
1346
1347 C89STR_ZERO_OBJECT(&row);
1348
1349 /* All we're doing is splitting on '|'. */
1350 pRunningStr += c89str_utf8_ltrim_offset(line, c89str_len(line)) + 1; /* Skip the initial '|' character. */
1351
1352 for (;;) {
1353 size_t loc;
1354
1355 if (c89str_find(pRunningStr, "|", &loc) != FS_SUCCESS) {
1356 break;
1357 }
1358
1359 row.cells[row.cellCount] = c89str_trim(c89str_newn(NULL, pRunningStr, loc), NULL);
1360 row.cellCount += 1;
1361
1362 pRunningStr += loc + 1; /* Plus one for the pipe character. */
1363 }
1364
1365 return row;
1366 }
1367
1368 table parse_table(const char* pText, size_t textLen)
1369 {
1370 table t;
1371 const char* pRunningText = pText;
1372 size_t thisLineLen;
1373 size_t nextLineBeg;
1374
1375 C89STR_ZERO_OBJECT(&t);
1376
1377 /* We just ignore the first row which should be starting with "+". */
1378 pRunningText += c89str_utf8_find_next_line(pRunningText, textLen, NULL);
1379
1380 /*
1381 There's two ways to generate define the rows in a table. The first is just one line equals one row. The other uses a separator to define the rows. To distinguish
1382 between the two we just need to count how many separators there are in the table. If there is 3, we can use the one line technique.
1383 */
1384 while (pRunningText < pText + textLen) {
1385 c89str line;
1386 size_t loff;
1387
1388 nextLineBeg = c89str_utf8_find_next_line(pRunningText, textLen - (pRunningText - pText), &thisLineLen);
1389
1390 line = c89str_newn(NULL, pRunningText, nextLineBeg); /* Intentionally using nextLineBeg instead of thisLineEnd so we can capture the new-line character. */
1391 loff = c89str_utf8_ltrim_offset(line, c89str_len(line));
1392
1393 if (line[loff] == '|') {
1394 table_row row = parse_table_row(line);
1395
1396 if (c89str_is_null_or_whitespace(row.cells[0], (size_t)-1)) {
1397 /* There's nothing in the first cell so we're just going to merge the rows. */
1398 merge_table_row(&t.rows[t.rowCount], &row);
1399 } else {
1400 /* It's a new row. Anything in the current row needs to be committed, and then the new row started. */
1401 if (!c89str_is_null_or_whitespace(t.rows[t.rowCount].cells[0], (size_t)-1)) {
1402 t.rowCount += 1; /* Commit the existing row if there's anything there at the moment. */
1403 }
1404 t.rows[t.rowCount] = row;
1405 }
1406 } else if (line[loff] == '+') {
1407 t.rowCount += 1; /* Commit the row. */
1408 }
1409
1410 pRunningText += nextLineBeg;
1411 }
1412
1413 /* The column count can be set to the cell count of the first row. */
1414 if (t.rowCount > 0) {
1415 t.colCount = t.rows[0].cellCount;
1416 }
1417
1418 return t;
1419 }
1420
1421 c89str raw_to_html_table(const char* pText, size_t textLen, size_t indentation)
1422 {
1423 c89str html = NULL;
1424
1425 (void)indentation;
1426
1427 html = c89str_cat(html, NULL, "<div style=\"overflow:hidden;\"><table class=\"doc\">");
1428 {
1429 /* We're doing to generate the table in two passes. The first is going to extract the contents of the table, the second will generate the HTML. */
1430 table t;
1431 size_t iRow;
1432 size_t iCol;
1433
1434 /* First step is to parse the table. */
1435 t = parse_table(pText, textLen);
1436
1437 /* Now that we have the table we can generate the HTML. */
1438 for (iRow = 0; iRow < t.rowCount; iRow += 1) {
1439 html = c89str_cat(html, NULL, "<tr>\n");
1440 {
1441 for (iCol = 0; iCol < t.colCount; iCol += 1) {
1442 html = c89str_catf(html, NULL, "<t%s class=\"doc\" valign=\"top\">", (iRow == 0) ? "h" : "d");
1443 html = c89str_cat(html, NULL, raw_to_html(t.rows[iRow].cells[iCol]));
1444 html = c89str_catf(html, NULL, "</t%s>\n", (iRow == 0) ? "h" : "d");
1445 }
1446 }
1447 html = c89str_cat(html, NULL, "</tr>\n");
1448 }
1449 }
1450 html = c89str_cat(html, NULL, "</table></div>");
1451
1452 return html;
1453 }
1454
1455 c89str raw_to_html_header(const char* pText, size_t textLen, size_t headerLevel)
1456 {
1457 c89str header = NULL;
1458
1459 header = c89str_catf(header, NULL, "<h%d id=\"%s\" class=\"man\">", (int)headerLevel, extract_manual_section_bookmark(pText, textLen));
1460 {
1461 header = c89str_catn(header, NULL, pText, textLen);
1462 }
1463 header = c89str_catf(header, NULL, "</h%d>\n", (int)headerLevel);
1464
1465 return header;
1466 }
1467
1468 c89str raw_to_html_list_item(const char* pText, size_t textLen, size_t itemCounter, int isLastItem)
1469 {
1470 c89str html = NULL;
1471
1472 if (itemCounter == 1) {
1473 html = c89str_cat(html, NULL, "<ul style=\"overflow:hidden;\">\n");
1474 }
1475
1476 html = c89str_cat(html, NULL, "<li>\n");
1477 {
1478 html = c89str_cat(html, NULL, escape_html(pText, textLen));
1479 }
1480 html = c89str_cat(html, NULL, "</li>\n");
1481
1482 if (isLastItem) {
1483 html = c89str_cat(html, NULL, "</ul>\n");
1484 }
1485
1486 return html;
1487 }
1488
1489 c89str raw_to_html(c89str raw)
1490 {
1491 int result;
1492 c89str html = NULL;
1493 doc_lexer lexer;
1494
1495 result = doc_lexer_init(raw, c89str_len(raw), &lexer);
1496 if (result != 0) {
1497 return NULL;
1498 }
1499
1500 for (;;) {
1501 result = doc_lexer_next(&lexer);
1502 if (result != 0) {
1503 break; /* We're done. */
1504 }
1505
1506 if (lexer.token == doc_token_type_header) {
1507 html = c89str_cat(html, NULL, raw_to_html_header(lexer.pTokenStr, lexer.tokenLen, lexer.headerLevel));
1508 } else if (lexer.token == doc_token_type_code) {
1509 html = c89str_cat(html, NULL, raw_to_html_code(lexer.pTokenStr, lexer.tokenLen, lexer.indentation, lexer.codeLang));
1510 } else if (lexer.token == doc_token_type_table) {
1511 html = c89str_cat(html, NULL, raw_to_html_table(lexer.pTokenStr, lexer.tokenLen, lexer.indentation));
1512 } else if (lexer.token == doc_token_type_list_item) {
1513 html = c89str_cat(html, NULL, raw_to_html_list_item(lexer.pTokenStr, lexer.tokenLen, lexer.listItemCounter, lexer.isLastListItem));
1514 } else {
1515 html = c89str_cat(html, NULL, raw_to_html_p(lexer.pTokenStr, lexer.tokenLen));
1516 }
1517 }
1518
1519 return html;
1520 }
1521
1522 c89str example_name_to_display(c89str name)
1523 {
1524 char* pRunningStr;
1525 c89str display = NULL;
1526 char temp[256];
1527 path_remove_extension(temp, sizeof(temp), name);
1528
1529 display = c89str_new(NULL, temp);
1530 display = c89str_replace_all(display, NULL, "_", (size_t)-1, " ", (size_t)-1);
1531
1532 /* We need to capitalize the first character of each word. */
1533 if (display[0] >= 'a' && display[0] <= 'z') {
1534 display[0] -= 32;
1535 }
1536
1537 /* For each remaining word. */
1538 pRunningStr = display;
1539 for (;;) {
1540 size_t next = c89str_find_next_whitespace(pRunningStr, (size_t)-1, NULL);
1541 if (next == c89str_npos) {
1542 break; /* We're done. */
1543 }
1544
1545 if (pRunningStr[next+1] >= 'a' && pRunningStr[next+1] <= 'z') {
1546 pRunningStr[next+1] -= 32;
1547 }
1548
1549 pRunningStr += next + 1;
1550 }
1551
1552 return display;
1553 }
1554
1555 c89str example_name_to_html_file_name(c89str name)
1556 {
1557 char html[4096];
1558
1559 path_remove_extension(html, sizeof(html), name);
1560 path_append_extension(html, sizeof(html), html, "html");
1561
1562 return c89str_new(NULL, html);
1563 }
1564
1565 c89str extract_example_summary_from_comment(c89str comment)
1566 {
1567 int result;
1568 doc_lexer lexer;
1569
1570 result = doc_lexer_init(comment, c89str_len(comment), &lexer);
1571 if (result != 0) {
1572 return c89str_new(NULL, ""); /* Failed to initialize lexer. */
1573 }
1574
1575 /* All we need to do is extract the first paragraph. */
1576 result = doc_lexer_next(&lexer);
1577 if (result != 0) {
1578 return c89str_new(NULL, "");
1579 }
1580
1581 return c89str_newn(NULL, lexer.pTokenStr, lexer.tokenLen);
1582 }
1583
1584 c89str extract_example_summary(c89str code)
1585 {
1586 int result;
1587 c89str_lexer lexer;
1588 c89str comment;
1589 c89str summary;
1590
1591 /* The summary is the first paragraph of the top section of the code which will be in a block comment. */
1592 result = c89str_lexer_init(&lexer, code, c89str_len(code));
1593 if (result != 0) {
1594 return c89str_new(NULL, ""); /* Failed. */
1595 }
1596
1597 /*
1598 We have the C lexer ready to go. We need to get the first block comment. To do this we just exclude whitespace and new lines and take the first
1599 token. If it's a comment, that'll be where we draw the summary from. Otherwise we'll just return an empty string.
1600 */
1601 lexer.options.skipNewlines = C89STR_TRUE;
1602 lexer.options.skipWhitespace = C89STR_TRUE;
1603
1604 result = c89str_lexer_next(&lexer);
1605 if (result != 0 || lexer.token != c89str_token_type_comment) {
1606 return c89str_new(NULL, ""); /* Failed to retrieve the first token. */
1607 }
1608
1609 /* We now want to format the comment in preparation for running it through the documentation lexer. */
1610 result = c89str_lexer_transform_token(&lexer, &comment, NULL);
1611 if (result != 0) {
1612 return c89str_new(NULL, ""); /* Failed to transform comment. */
1613 }
1614
1615 /* Trim the comment to ensure all leading whitespace and new line characters are excluded. */
1616 comment = c89str_trim(comment, NULL);
1617
1618 /* We now have enough information to extract the summary from the comment. */
1619 summary = c89str_trim(extract_example_summary_from_comment(comment), NULL);
1620 c89str_delete(comment, NULL);
1621
1622 return summary;
1623 }
1624
1625 c89str navigation_to_html(docstate* pState, doc_category category, const char* pEntityName)
1626 {
1627 c89str html = NULL;
1628
1629 (void)pState;
1630
1631 if (category == doc_category_home) {
1632 html = c89str_cat(html, NULL, "<a href=\"{{ relative-path \"docs/index.html\" }}\" class=\"doc-navigation doc-navigation-active\">Documentation Home</a>");
1633 } else {
1634 html = c89str_cat(html, NULL, "<a href=\"{{ relative-path \"docs/index.html\" }}\" class=\"doc-navigation\">Documentation Home</a>");
1635 }
1636
1637 if (category == doc_category_manual) {
1638 size_t iSection;
1639
1640 html = c89str_cat(html, NULL, "<a href=\"{{ relative-path \"docs/manual/index.html\" }}\" class=\"doc-navigation doc-navigation-active\">Programming Manual</a>");
1641
1642 for (iSection = 0; iSection < pState->manual.sectionCount; iSection += 1) {
1643 if (pState->manual.sections[iSection].level == 1) {
1644 html = c89str_catf(html, NULL, "<a href=\"#%s\" class=\"doc-navigation doc-navigation-l%d\">%s</a>", pState->manual.sections[iSection].bookmark, (int)pState->manual.sections[iSection].level, pState->manual.sections[iSection].title);
1645 }
1646 }
1647 } else {
1648 html = c89str_cat(html, NULL, "<a href=\"{{ relative-path \"docs/manual/index.html\" }}\" class=\"doc-navigation\">Programming Manual</a>");
1649 }
1650
1651 if (category == doc_category_examples) {
1652 size_t iExample;
1653
1654 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"./docs/examples/index.html\" }}\" class=\"doc-navigation %s\">Examples</a>", (pEntityName == NULL) ? "doc-navigation-active" : "");
1655
1656 for (iExample = 0; iExample < pState->examples.count; iExample += 1) {
1657 c89str_bool32 isActive = C89STR_FALSE;
1658 if (pEntityName != NULL && strcmp(pEntityName, pState->examples.examples[iExample].name) == 0) {
1659 isActive = C89STR_TRUE;
1660 }
1661
1662 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/examples/%s\" }}\" class=\"doc-navigation doc-navigation-l%d %s\">%s</a>", example_name_to_html_file_name(pState->examples.examples[iExample].name), (int)1, (isActive) ? "doc-navigation-active" : "", example_name_to_display(pState->examples.examples[iExample].name));
1663 }
1664 } else {
1665 html = c89str_cat(html, NULL, "<a href=\"{{ relative-path \"docs/examples/index.html\" }}\" class=\"doc-navigation\">Examples</a>");
1666 }
1667
1668 if (category == doc_category_api) {
1669 html = c89str_cat(html, NULL, "<a href=\"{{ relative-path \"docs/api/index.html\" }}\" class=\"doc-navigation doc-navigation-active\">API Reference</a>");
1670 } else {
1671 html = c89str_cat(html, NULL, "<a href=\"{{ relative-path \"docs/api/index.html\" }}\" class=\"doc-navigation\" style=\"border-bottom:none;\">API Reference</a>");
1672 }
1673
1674 return html;
1675 }
1676
1677 c89str generate_html_generic(docstate* pState, const char* pNavitationHTML, const char* pBodyHTML)
1678 {
1679 c89str html = NULL;
1680
1681 (void)pState;
1682
1683 html = c89str_cat(html, NULL, "{{ miniaudio-header }}\n");
1684 {
1685 html = c89str_cat(html, NULL, "<table border=\"0\" style=\"margin:0 auto; width:100%; border-collapse:collapse; border:solid 0px #000; table-layout:fixed;\"><tr>\n");
1686 {
1687 /* Navigation panel. */
1688 html = c89str_cat(html, NULL, "<td valign=\"top\" style=\"width:20em; padding:0; margin:0; border-right:solid 0px #000;\"><div style=\"position:relative; height:100%; width:100%; border:solid 0px #000; padding:0; margin:0;\">\n");
1689 {
1690 html = c89str_cat(html, NULL, pNavitationHTML);
1691 }
1692 html = c89str_cat(html, NULL, "</div></td>");
1693
1694 /* Body. */
1695 html = c89str_cat(html, NULL, "<td valign=\"top\" style=\"padding:1em; border-left:solid 1px #bbb;\">\n");
1696 {
1697 html = c89str_cat(html, NULL, pBodyHTML);
1698 }
1699 html = c89str_cat(html, NULL, "</td>");
1700 }
1701 html = c89str_cat(html, NULL, "\n</tr></table>");
1702 }
1703 html = c89str_cat(html, NULL, "\n{{ miniaudio-footer }}");
1704
1705 return html;
1706 }
1707
1708
1709 static const char* g_HTMLBannerImage = "<div style=\"text-align:center; overflow:hidden;\"><img src=\"{{ relative-path \"img/logo1_large.png\" }}\" style=\"width:auto; height:auto; min-height:70px; overflow:hidden;\"></div>";
1710
1711 c89str generate_home_index_html(docstate* pState)
1712 {
1713 c89str html = NULL;
1714
1715 (void)pState;
1716
1717 html = c89str_cat(html, NULL, "<div style=\"text-align:center; padding:1em;\">");
1718 {
1719 html = c89str_cat(html, NULL, g_HTMLBannerImage);
1720 html = c89str_cat(html, NULL, "<div style=\"padding-top:1em; font-weight:bold; font-size:2em; color:#444;\">Documentation</div>");
1721 html = c89str_cat(html, NULL, "<div style=\"padding-top:0.75em; text-align:center;\">");
1722 {
1723 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/manual/index.html\" }}\">Programming Manual</a> - ");
1724 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/examples/index.html\" }}\">Examples</a> - ");
1725 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/api/index.html\" }}\">API Reference</a> - ");
1726 html = c89str_catf(html, NULL, "<a href=\"https://github.com/mackron/miniaudio\">Source Code</a>");
1727 }
1728 html = c89str_cat(html, NULL, "</div>");
1729 }
1730 html = c89str_cat(html, NULL, "</div>");
1731
1732 return html;
1733 }
1734
1735 c89str generate_home_html(docstate* pState)
1736 {
1737 return generate_html_generic(pState, navigation_to_html(pState, doc_category_home, NULL), generate_home_index_html(pState));
1738 }
1739
1740
1741 c89str generate_manual_html(docstate* pState)
1742 {
1743 c89str html = NULL;
1744
1745 html = c89str_cat(html, NULL, "<div style=\"text-align:center; padding:1em; padding-bottom:2em;\">");
1746 {
1747 html = c89str_cat(html, NULL, g_HTMLBannerImage);
1748 html = c89str_cat(html, NULL, "<div style=\"padding-top:1em; font-weight:bold; font-size:2em; color:#444;\">Programming Manual</div>");
1749 html = c89str_cat(html, NULL, "<div style=\"padding-top:0.75em; text-align:center;\">");
1750 {
1751 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/examples/index.html\" }}\">Examples</a> - ");
1752 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/api/index.html\" }}\">API Reference</a> - ");
1753 html = c89str_catf(html, NULL, "<a href=\"https://github.com/mackron/miniaudio\">Source Code</a>");
1754 }
1755 html = c89str_cat(html, NULL, "</div>");
1756 }
1757 html = c89str_cat(html, NULL, "</div>\n");
1758
1759 html = c89str_cat(html, NULL, raw_to_html(pState->manual.raw));
1760
1761 return generate_html_generic(pState, navigation_to_html(pState, doc_category_manual, NULL), html);
1762 }
1763
1764
1765 c89str generate_example_index_body(docstate* pState)
1766 {
1767 c89str html = NULL;
1768
1769 html = c89str_cat(html, NULL, "<div style=\"text-align:center; padding:1em; padding-bottom:2em;\">");
1770 {
1771 html = c89str_cat(html, NULL, g_HTMLBannerImage);
1772 html = c89str_cat(html, NULL, "<div style=\"padding-top:1em; font-weight:bold; font-size:2em; color:#444;\">Examples</div>");
1773 html = c89str_cat(html, NULL, "<div style=\"padding-top:0.75em; text-align:center;\">");
1774 {
1775 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/manual/index.html\" }}\">Programming Manual</a> - ");
1776 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/api/index.html\" }}\">API Reference</a> - ");
1777 html = c89str_catf(html, NULL, "<a href=\"https://github.com/mackron/miniaudio\">Source Code</a>");
1778 }
1779 html = c89str_cat(html, NULL, "</div>");
1780 }
1781 html = c89str_cat(html, NULL, "</div>\n");
1782
1783 /* At the moment this is just a list of examples. */
1784 html = c89str_cat(html, NULL, "<table style=\"border:none;\">");
1785 {
1786 size_t iExample;
1787 for (iExample = 0; iExample < pState->examples.count; iExample += 1) {
1788 html = c89str_cat(html, NULL, "<tr>");
1789 {
1790 /* Name and URL. */
1791 html = c89str_cat(html, NULL, "<td style=\"padding-right:2em;\">");
1792 {
1793 html = c89str_catf(html, NULL, "<a href=\"%s\">%s</a>", example_name_to_html_file_name(pState->examples.examples[iExample].name), example_name_to_display(pState->examples.examples[iExample].name));
1794 }
1795 html = c89str_cat(html, NULL, "</td>");
1796
1797 /* Summary. */
1798 html = c89str_cat(html, NULL, "<td>");
1799 {
1800 html = c89str_cat(html, NULL, extract_example_summary(pState->examples.examples[iExample].code)); // example_name_to_display(pState->examples.examples[iExample].name)); /* TODO: Change this to the summary. */
1801 }
1802 html = c89str_cat(html, NULL, "</td>");
1803 }
1804 html = c89str_cat(html, NULL, "</tr>");
1805 }
1806 }
1807 html = c89str_cat(html, NULL, "</table>");
1808
1809 return html;
1810 }
1811
1812 c89str generate_example_index_html(docstate* pState)
1813 {
1814 return generate_html_generic(pState, navigation_to_html(pState, doc_category_examples, NULL), generate_example_index_body(pState));
1815 }
1816
1817 c89str generate_example_html_body(docstate* pState, const doc_example* pExample)
1818 {
1819 c89str html = NULL;
1820 c89str_lexer lexer;
1821 const char* pCodeStart = pExample->code;
1822
1823 (void)pState;
1824
1825 html = c89str_catf(html, NULL, "<h1>%s</h1>", example_name_to_display(pExample->name));
1826 {
1827 /* We use a C lexer to extract the top section which is in a comment. We then convert this to HTML. */
1828 int result = c89str_lexer_init(&lexer, pExample->code, c89str_len(pExample->code));
1829 if (result == 0) {
1830 c89str comment;
1831
1832 lexer.options.skipWhitespace = C89STR_TRUE;
1833 lexer.options.skipNewlines = C89STR_TRUE;
1834
1835 result = c89str_lexer_next(&lexer);
1836 if (result == 0 && lexer.token == c89str_token_type_comment) {
1837 result = c89str_lexer_transform_token(&lexer, &comment, NULL);
1838 if (result == 0) {
1839 comment = c89str_trim(comment, NULL);
1840 html = c89str_cat(html, NULL, raw_to_html(comment));
1841 c89str_delete(comment, NULL);
1842
1843 pCodeStart = lexer.pText + lexer.textOff;
1844 }
1845 }
1846 }
1847 }
1848 html = c89str_cat(html, NULL, raw_to_html_code(pCodeStart + c89str_utf8_ltrim_offset(pCodeStart, (size_t)-1), (size_t)-1, 0, doc_lang_c));
1849
1850 return html;
1851 }
1852
1853 c89str generate_example_html(docstate* pState, const doc_example* pExample)
1854 {
1855 c89str body = NULL;
1856 c89str html = NULL;
1857
1858 body = generate_example_html_body(pState, pExample);
1859
1860 html = generate_html_generic(pState, navigation_to_html(pState, doc_category_examples, pExample->name), body);
1861 c89str_delete(body, NULL);
1862
1863 return html;
1864 }
1865
1866 int generate_examples(docstate* pState)
1867 {
1868 int result;
1869 size_t iExample;
1870
1871 /* Examples home page. This is basically just a list of examples. */
1872 result = save_file(WEBSITE_DOCS_EXAMPLES_FOLDER"/index.html", generate_example_index_html(pState));
1873 if (result != 0) {
1874 return result; /* Failed to output the examples index page. */
1875 }
1876
1877 for (iExample = 0; iExample < pState->examples.count; iExample += 1) {
1878 /* The file path is the same as the file name, only with .html as the extension. */
1879 char filePath[4096];
1880
1881 fs_path_append (filePath, sizeof(filePath), WEBSITE_DOCS_EXAMPLES_FOLDER, FS_NULL_TERMINATED, pState->examples.examples[iExample].name, FS_NULL_TERMINATED);
1882 path_remove_extension(filePath, sizeof(filePath), filePath);
1883 path_append_extension(filePath, sizeof(filePath), filePath, "html");
1884
1885 result = save_file(filePath, generate_example_html(pState, &pState->examples.examples[iExample]));
1886 if (result != 0) {
1887 return result; /* Failed to save the file. */
1888 }
1889 }
1890
1891 return 0;
1892 }
1893
1894
1895 c89str generate_api_index_body(docstate* pState)
1896 {
1897 c89str html = NULL;
1898
1899 (void)pState;
1900
1901 html = c89str_cat(html, NULL, "<div style=\"text-align:center; padding:1em; padding-bottom:2em;\">");
1902 {
1903 html = c89str_cat(html, NULL, g_HTMLBannerImage);
1904 html = c89str_cat(html, NULL, "<div style=\"padding-top:1em; font-weight:bold; font-size:2em; color:#444;\">API Reference</div>");
1905 html = c89str_cat(html, NULL, "<div style=\"padding-top:0.75em; text-align:center;\">");
1906 {
1907 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/manual/index.html\" }}\">Programming Manual</a> - ");
1908 html = c89str_catf(html, NULL, "<a href=\"{{ relative-path \"docs/examples/index.html\" }}\">Examples</a> - ");
1909 html = c89str_catf(html, NULL, "<a href=\"https://github.com/mackron/miniaudio\">Source Code</a>");
1910 }
1911 html = c89str_cat(html, NULL, "</div>");
1912 }
1913 html = c89str_cat(html, NULL, "</div>\n");
1914
1915 html = c89str_cat(html, NULL, "Coming soon...");
1916
1917 return html;
1918 }
1919
1920 c89str generate_api_index_html(docstate* pState)
1921 {
1922 return generate_html_generic(pState, navigation_to_html(pState, doc_category_api, NULL), generate_api_index_body(pState));
1923 }
1924
1925 int generate_api(docstate* pState)
1926 {
1927 int result;
1928
1929 /* API home page. This is basically just a list of examples. */
1930 result = save_file(WEBSITE_DOCS_API_FOLDER"/index.html", generate_api_index_html(pState));
1931 if (result != 0) {
1932 return result; /* Failed to output the examples index page. */
1933 }
1934
1935 /* TODO: Generate API files. */
1936
1937 return 0;
1938 }
1939
1940
1941 int generate(docstate* pState)
1942 {
1943 int result;
1944
1945
1946 fs_rmdir_content(WEBSITE_DOCS_FOLDER);
1947
1948
1949 /* Home */
1950 result = save_file(WEBSITE_DOCS_FOLDER"/index.html", generate_home_html(pState));
1951 if (result != 0) {
1952 return result;
1953 }
1954
1955
1956 /* Manual */
1957 result = save_file(WEBSITE_DOCS_MANUAL_FOLDER"/index.html", generate_manual_html(pState));
1958 if (result != 0) {
1959 return result;
1960 }
1961
1962
1963 /* Examples */
1964 result = generate_examples(pState);
1965 if (result != 0) {
1966 return result;
1967 }
1968
1969
1970 /* API */
1971 result = generate_api(pState);
1972 if (result != 0) {
1973 return result;
1974 }
1975
1976
1977 /* We're done. */
1978 return 0;
1979 }
1980
1981
1982 int main(int argc, char** argv)
1983 {
1984 int result;
1985 docstate state;
1986
1987 (void)argc;
1988 (void)argv;
1989
1990 result = load(&state);
1991 if (result != 0) {
1992 return result;
1993 }
1994
1995 /*
1996 NOTE:
1997
1998 There's a weird permission error going on with my NAS. For now, you need to generate the website separately,
1999 and then generate the output via webplate as a separate step. In addition, clearing the output directory with
2000 WEBPLATE_FLAG_CLEAR_OUTDIR will sometimes fail. To address this, manually delete the output directory and run
2001 webplate again.
2002 */
2003 #if 1
2004 {
2005 result = generate(&state);
2006 if (result != 0) {
2007 return result;
2008 }
2009 }
2010 #endif
2011
2012 /* Generate the final website with webplate. */
2013 if (webplate_process("website", "../miniaud.io", WEBPLATE_FLAG_CLEAR_OUTDIR | 0) != WEBPLATE_SUCCESS) {
2014 printf("Failed to generate website via webplate.");
2015 }
2016
2017 return 0;
2018 }