Repository navigation
Expand file tree
/
Copy pathmadoc.c
More file actions
2017 lines (1619 loc) · 65.6 KB
/
Copy pathmadoc.c
File metadata and controls
2017 lines (1619 loc) · 65.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This is very rough and built specifically for miniaudio. Don't get clever and try using this for
your own project because it will most likely not work.
*/
#include "../../../fs/fs.c"
#define C89STR_IMPLEMENTATION
#include "../../../c89str/c89str.h"
/*
This runs in two phases. The first phase generates a webplate-compatible template site. The second phase
uses webplate to actually generate the actual website.
*/
#include "../../../webplate/source/libwebplate.c"
/* The output directory, relative to the tools/build/_bin directory. */
#define WEBSITE_DOCS_FOLDER "website/docs"
#define WEBSITE_DOCS_MANUAL_FOLDER WEBSITE_DOCS_FOLDER"/manual"
#define WEBSITE_DOCS_EXAMPLES_FOLDER WEBSITE_DOCS_FOLDER"/examples"
#define WEBSITE_DOCS_API_FOLDER WEBSITE_DOCS_FOLDER"/api"
#define EXAMPLES_FOLDER "examples"
c89str load_file(const char* pFilePath)
{
fs_result resultFS;
size_t fileSize;
char* pFileData;
c89str str;
resultFS = fs_file_open_and_read(NULL, pFilePath, FS_FORMAT_TEXT, (void**)&pFileData, &fileSize);
if (resultFS != FS_SUCCESS) {
printf("Failed to open %s: %s.\n", pFilePath, fs_result_description(resultFS));
return NULL;
}
/* We have the file data, but now we need to convert it to a dynamic string so we can manipulate it later. */
str = c89str_newn(NULL, pFileData, fileSize);
fs_free(pFileData, NULL);
return str;
}
int save_file(const char* pFilePath, c89str src)
{
fs_result resultFS;
resultFS = fs_file_open_and_write(NULL, pFilePath, src, c89str_len(src));
if (resultFS != FS_SUCCESS) {
printf("Failed to save %s: %s.\n", pFilePath, fs_result_description(resultFS));
return (int)resultFS;
}
return 0;
}
c89str convert_tabs_to_spaces(c89str str)
{
/* Assume a tab is 4 spaces. */
return c89str_replace_all(str, NULL, "\t", 1, " ", 4);
}
c89str strip_code_block_comments(c89str str)
{
size_t runningOffset = 0;
if (str == NULL) {
return str;
}
/*
Block comments are not recursive which slightly simplifies our algorithm. The algorithm we're using here is not
based on speed and can be made much more efficient. Note that this will stip blocks that contain an opening or
closing block within a string constant. The proper way to do this is to use a proper C parser, but that is more
than what we need right now.
*/
for (;;) {
size_t openingOffset;
size_t closingOffset;
/* Opening. */
if (c89str_find(str + runningOffset, "/*", &openingOffset) != C89STR_SUCCESS) {
break; /* We're done. */
}
openingOffset += runningOffset; /* Normalize the opening offset. */
/* Closing. */
if (c89str_find(str + openingOffset, "*/", &closingOffset) != C89STR_SUCCESS) {
break; /* We're done. */
}
closingOffset += openingOffset; /* Normalize the closing offset. */
closingOffset += 2; /* Make sure to include the closing block itself. */
/* We now have enough information to delete the comment block. */
str = c89str_replace(str, NULL, openingOffset, (closingOffset - openingOffset), "", 0);
if (str == NULL) {
return str;
}
/* We're done with this block so we can now move on to the next. */
runningOffset = openingOffset;
}
return str;
}
c89str strip_code_line_comments(c89str str)
{
if (str == NULL) {
return str;
}
/* TODO: Implement me. */
return str;
}
c89str strip_code_comments(c89str str)
{
return strip_code_line_comments(strip_code_block_comments(str));
}
c89str strip_empty_lines(c89str str)
{
const char* pRunningStr;
c89str newstr = NULL;
size_t lineLen;
size_t nextLineOffset;
if (str == NULL) {
return str;
}
pRunningStr = str;
/* We are going to fully recreate the string line by line. We can allocate enough space from the start. */
newstr = c89str_new_with_cap(NULL, c89str_len(str));
if (newstr == NULL) {
return str;
}
while (pRunningStr[0] != '\0') {
nextLineOffset = c89str_utf8_find_next_line(pRunningStr, (size_t)-1, &lineLen);
if (c89str_is_null_or_whitespace(pRunningStr, lineLen) == C89STR_FALSE) {
newstr = c89str_catn(newstr, NULL, pRunningStr, nextLineOffset);
}
if (nextLineOffset == c89str_npos) {
break;
} else {
pRunningStr += nextLineOffset;
}
}
if (newstr == NULL) {
return str;
}
c89str_delete(str, NULL);
return newstr;
}
c89str strip_trailing_whitespace(c89str str)
{
const char* pRunningStr;
c89str newstr = NULL;
if (str == NULL) {
return str;
}
pRunningStr = str;
/* We are going to fully recreate the string line by line. We can allocate enough space from the start. */
newstr = c89str_new_with_cap(NULL, c89str_len(str));
if (newstr == NULL) {
return str;
}
while (pRunningStr[0] != '\0') {
size_t lineLen;
size_t nextLineOffset;
nextLineOffset = c89str_utf8_find_next_line(pRunningStr, (size_t)-1, &lineLen);
newstr = c89str_catn(newstr, NULL, pRunningStr, c89str_utf8_rtrim_offset(pRunningStr, lineLen));
/* Insert the new line character. */
if (nextLineOffset != c89str_npos) {
newstr = c89str_catn(newstr, NULL, pRunningStr + lineLen, nextLineOffset - lineLen);
} else {
newstr = c89str_catn(newstr, NULL, pRunningStr + lineLen, lineLen);
}
if (nextLineOffset == c89str_npos) {
break;
} else {
pRunningStr += nextLineOffset;
}
}
if (newstr == NULL) {
return str;
}
c89str_delete(str, NULL);
return newstr;
}
c89str strip_whitespace(c89str str)
{
return strip_trailing_whitespace(strip_empty_lines(str));
}
c89str minify_code(c89str str)
{
return convert_tabs_to_spaces(strip_whitespace(strip_code_comments(str)));
}
static int path_remove_extension(char* dst, size_t dstSizeInBytes, const char* src)
{
const char* ext;
if (src == NULL) {
if (dst != NULL && dstSizeInBytes > 0) {
dst[0] = '\0';
}
return -1;
}
ext = fs_path_extension(src, FS_NULL_TERMINATED);
if (ext == NULL || ext[0] == '\0') {
/* No extension. */
if (dst == src) {
return (ext - dst);
} else {
size_t len = strlen(src);
fs_strncpy_s(dst, dstSizeInBytes, src, len);
return (int)len;
}
} else {
/* Have extension. */
size_t dstLen = (size_t)(ext - src - 1); /* -1 for the period. */
if (dst == src) {
dst[dstLen] = '\0';
} else {
fs_strncpy_s(dst, dstSizeInBytes, src, dstLen);
}
return dstLen;
}
}
static int path_append_extension(char* dst, size_t dstSizeInBytes, const char* base, const char* extension)
{
fs_result result = FS_SUCCESS;
size_t baseLength;
size_t extLength;
size_t dstLen;
if (base == NULL) {
base = "";
}
if (extension == NULL) {
extension = "";
}
if (extension[0] == '\0') {
if (dst != NULL) {
if (dst != base) {
fs_strcpy_s(dst, dstSizeInBytes, base);
}
}
return (int)strlen(base);
}
baseLength = strlen(base);
extLength = strlen(extension);
dstLen = baseLength + 1 + extLength;
if (dst != NULL) {
if (dstLen+1 <= dstSizeInBytes) {
if (dst != base) {
fs_strcpy_s(dst + 0, dstSizeInBytes - 0, base);
}
fs_strcpy_s(dst + baseLength, dstSizeInBytes - baseLength, ".");
fs_strcpy_s(dst + baseLength + 1, dstSizeInBytes - baseLength - 1, extension);
}
}
return (int)dstLen;
}
static fs_result fs_rmdir_content(const char* pDirectory)
{
/* We'll use an iterator for this. */
fs_iterator* pIterator;
if (pDirectory == NULL) {
return FS_INVALID_ARGS;
}
pIterator = fs_first(NULL, pDirectory, FS_READ);
while (pIterator != NULL) {
char* pFilePath;
int filePathLen;
/* Get the length first. */
filePathLen = fs_path_append(NULL, 0, pDirectory, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
if (filePathLen > 0) {
pFilePath = (char*)fs_malloc((size_t)filePathLen + 1, NULL); /* +1 for null terminator. */
if (pFilePath != NULL) {
fs_path_append(pFilePath, (size_t)filePathLen + 1, pDirectory, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
if (pIterator->info.directory) {
if (pIterator->pName[0] == '.' && pIterator->pName[1] == '\0') {
/* "." - ignore. */
} else if (pIterator->pName[0] == '.' && pIterator->pName[1] == '.' && pIterator->pName[2] == '\0') {
/* ".." - ignore. */
} else {
fs_rmdir_content(pFilePath);
}
}
fs_remove(NULL, pFilePath, 0);
} else {
return FS_OUT_OF_MEMORY;
}
} else {
/* Failed to retrieve the file path length. */
}
pIterator = fs_next(pIterator);
}
fs_free_iterator(pIterator);
return FS_SUCCESS;
}
typedef struct
{
c89str name;
c89str code;
} doc_example;
typedef enum
{
doc_category_home,
doc_category_manual,
doc_category_examples,
doc_category_api
} doc_category;
typedef enum
{
doc_token_type_paragraph,
doc_token_type_code,
doc_token_type_table,
doc_token_type_header,
doc_token_type_list_item
} doc_token_type;
typedef enum
{
doc_lang_none,
doc_lang_c
} doc_lang;
typedef struct
{
const char* pText;
size_t textLen; /* Set to (size_t)-1 for null terminated. */
size_t textOff; /* The cursor. */
doc_token_type token;
const char* pTokenStr;
size_t tokenLen;
size_t headerLevel;
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. */
size_t prevLineOff;
size_t prevLineLen;
doc_lang codeLang;
int isLastListItem;
size_t listItemCounter;
} doc_lexer;
int doc_lexer_init(const char* pText, size_t textLen, doc_lexer* pLexer)
{
if (pText == NULL || pLexer == NULL) {
return EINVAL;
}
C89STR_ZERO_OBJECT(pLexer);
if (textLen == (size_t)-1) {
textLen = c89str_strlen(pText);
}
pLexer->pText = pText;
pLexer->textLen = textLen;
pLexer->textOff = 0;
return 0;
}
static int doc_lexer_is_header_underline(const char* pText, size_t textLen, char ch)
{
size_t i;
if (textLen == 0) {
return 0;
}
for (i = 0; i < textLen; i += 1) {
if (pText[i] == '\0') {
break;
}
if (pText[i] != ch) {
return 0;
}
}
/* Getting here means it's an underline. */
return 1;
}
static size_t doc_lexer_get_header_underline_level(const char* pText, size_t textLen)
{
if (doc_lexer_is_header_underline(pText, textLen, '-')) {
return 2;
}
if (doc_lexer_is_header_underline(pText, textLen, '=')) {
return 1;
}
return 0;
}
int doc_lexer_next(doc_lexer* pLexer)
{
//int result;
const char* txt;
size_t off;
size_t len;
if (pLexer == NULL) {
return EINVAL;
}
txt = pLexer->pText;
off = pLexer->textOff; /* Moves forward. */
len = pLexer->textLen; /* Constant. */
/* We run line-by-line. */
for (;;) {
size_t thisLineLen;
size_t nextLineBeg;
size_t indentation;
size_t headerLevel;
size_t location; /* When searching for a token on a line. */
if (txt[off] == '\0' || len == off) {
/* We're done. The last paragraph needs to be added, if any. */
if (off > pLexer->textOff) {
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
return 0;
} else {
return ENOMEM; /* We're done. */
}
}
/* Grab the next line. */
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
/* Check if we have a header underline. If so, we want to end the paragraph, if any, and then return. */
headerLevel = doc_lexer_get_header_underline_level(txt + off, thisLineLen);
if (headerLevel > 0) {
if (pLexer->textOff < pLexer->prevLineOff) {
/* We have a paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = pLexer->prevLineOff - pLexer->textOff;
pLexer->textOff = off;
} else {
/* We don't have a paragraph. Just post the header itself. */
pLexer->token = doc_token_type_header;
pLexer->pTokenStr = txt + pLexer->prevLineOff;
pLexer->tokenLen = pLexer->prevLineLen;
pLexer->textOff = off + nextLineBeg;
pLexer->headerLevel = headerLevel;
}
return 0;
}
/* Getting here means it's not a header. */
indentation = c89str_utf8_ltrim_offset(txt + off, len - off);
if (c89str_findn(txt + off, thisLineLen, "```", (size_t)-1, &location) == C89STR_SUCCESS) {
/* The beginning of a code block. If there's a pending paragraph that needs to be posted. */
if (pLexer->textOff < off) {
/* We have a paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
} else {
/* We don't have a paragraph. We can now parse the code block. */
size_t tokenOff = off;
size_t tokenLen = 0;
doc_lang lang = doc_lang_none;
if (txt[off + location + 3] == 'c') {
lang = doc_lang_c;
}
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
tokenOff = off + nextLineBeg;
tokenLen = 0;
off += nextLineBeg;
/* We now need to keep looping over each line until we find the closing the code block. */
for (;;) {
if (txt[off] == '\0' || off == len) {
break;
}
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
if (c89str_findn(txt + off, thisLineLen, "```", (size_t)-1, &location) == FS_SUCCESS) {
/* We found the end of the code block. */
pLexer->token = doc_token_type_code;
pLexer->pTokenStr = txt + tokenOff;
pLexer->tokenLen = tokenLen;
pLexer->textOff = off + nextLineBeg;
pLexer->indentation = indentation;
pLexer->codeLang = lang;
break;
} else {
}
off += nextLineBeg;
tokenLen += nextLineBeg;
}
}
return 0;
}
/* Getting here means it's not a header nor a code block. It might be a table. */
if (c89str_findn(txt + off, thisLineLen, "+--", (size_t)-1, &location) == FS_SUCCESS) {
/* The beginning of a table. The end of the table is the end of the last line that starts with a "+" or "|". */
if (pLexer->textOff < off) {
/* We have a paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
} else {
/* We don't have a paragraph. We can now parse the table block. */
size_t tokenOff = off;
size_t tokenLen = 0;
for (;;) {
if (txt[off] == '\0' || off == len) {
break;
}
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
if (nextLineBeg < 4 || (txt[off + indentation] != '+' && txt[off + indentation] != '|')) {
/* We found the end of the table. The end of the table is the end of this line. */
pLexer->token = doc_token_type_table;
pLexer->pTokenStr = txt + tokenOff;
pLexer->tokenLen = tokenLen;
pLexer->textOff = off + nextLineBeg;
pLexer->indentation = indentation;
break;
} else {
/* We're still looking at the table. */
}
off += nextLineBeg;
tokenLen += nextLineBeg;
}
}
return 0;
}
/* Check if it's a bullet point. */
if (txt[off + indentation] == '-' || txt[off + indentation] == '*') {
if (pLexer->textOff < off) {
/* There's a pending paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
} else {
/* A bullet point can cover multiple lines. The bullet point ends when a line starts with another bullet point or is empty. */
size_t tokenOff = off + indentation + 2;
size_t tokenLen = 0;
//int isLineBlank = 0;
int isNextLineBlank = 0;
off += indentation + 2;
/* Keep going until we find the start of a new bullet point or an empty line. */
for (;;) {
int foundEndOfBulletPoint = 0;
if (txt[off] == '\0' || off == len) {
break; /* At end. */
}
/* Go to the next line. */
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
/* Determine whether or not the next line is blank. */
isNextLineBlank = c89str_is_null_or_whitespace(txt + off + nextLineBeg, c89str_utf8_find_next_line(txt + off + nextLineBeg, len - off - nextLineBeg, NULL));
if (isNextLineBlank) {
foundEndOfBulletPoint = 1;
} else {
/* The next line is not blank so now check if it starts with a bullet point. */
size_t lineContentOff = off + nextLineBeg + c89str_utf8_ltrim_offset(txt + off + nextLineBeg, len - off - nextLineBeg);
if (txt[lineContentOff] == '-' || txt[lineContentOff] == '*' || txt[lineContentOff] == '\0') {
foundEndOfBulletPoint = 1;
}
}
if (foundEndOfBulletPoint) {
/* We found the end of the bullet point. Update the lexer and return. */
pLexer->token = doc_token_type_list_item;
pLexer->pTokenStr = txt + tokenOff;
pLexer->tokenLen = tokenLen + thisLineLen;
pLexer->textOff = off + nextLineBeg;
pLexer->indentation = indentation;
/* Reset the list item counter if necessary. */
if (pLexer->isLastListItem) {
pLexer->listItemCounter = 0;
}
pLexer->listItemCounter += 1;
pLexer->isLastListItem = isNextLineBlank;
return 0;
} else {
/* We didn't find the end of the bullet point. Move forward. */
off += indentation + nextLineBeg;
tokenLen += indentation + nextLineBeg;
}
}
}
return 0;
}
/* Not a header underline, code block or table. Assume a paragraph. If we have an empty line it means we can terminate our paragraph. */
if (c89str_is_null_or_whitespace(txt + off, thisLineLen)) {
if (pLexer->textOff < off) {
/* We have a paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
if (!c89str_is_null_or_whitespace(pLexer->pTokenStr, pLexer->tokenLen)) {
return 0;
}
}
}
/* We need to keep track of the previous line for the purpose of header underlines. */
pLexer->prevLineOff = off;
pLexer->prevLineLen = thisLineLen;
/* We're done. We can move to the next line now. */
off += nextLineBeg;
}
/*return 0;*/
}
typedef struct
{
c89str title;
c89str bookmark;
size_t level;
} manual_section;
typedef struct
{
struct
{
c89str raw;
size_t sectionCount;
manual_section sections[256];
} manual;
struct
{
size_t count;
doc_example examples[256];
} examples;
} docstate;
c89str extract_manual_section_title(const char* pStr, size_t len, size_t* pLevel)
{
if (pLevel != NULL) {
*pLevel = 0;
}
/* We want to remove the numbers because we have limited horizontal space. */
while (pStr[0] != '\0' && len > 0) {
if ((pStr[0] >= '0' && pStr[0] <= '9') || pStr[0] == '.') {
pStr += 1;
len -= 1;
if (pStr[0] == '.' && pLevel != NULL) {
*pLevel += 1;
}
continue;
} else {
break;
}
}
return c89str_trim(c89str_newn(NULL, pStr, len), NULL);
}
c89str extract_manual_section_bookmark(const char* pStr, size_t len)
{
return c89str_replace_all(extract_manual_section_title(pStr, len, NULL), NULL, " ", 1, "-", 0);
}
int load_manual(c89str miniaudio, docstate* pState)
{
int result;
c89str_lexer lexer;
int commentCounter;
result = c89str_lexer_init(&lexer, miniaudio, c89str_len(miniaudio));
if (result != 0) {
return result;
}
/* The manual will be in the second comment block. The first comment block is the project summary. */
commentCounter = 0;
for (;;) {
result = c89str_lexer_next(&lexer);
if (result != 0) {
return result;
}
if (lexer.token == c89str_token_type_comment) {
commentCounter += 1;
}
if (commentCounter == 2) {
/* We've found the comment block with the content of the programming manual. We just need to remove the enveloping comment tokens. */
c89str_lexer_transform_token(&lexer, &pState->manual.raw, NULL);
if (pState->manual.raw == NULL) {
return ENOMEM;
} else {
break;
}
}
}
/* We need to extract all the sections of the manual for the purpose of the navigation panel. */
{
doc_lexer docLexer;
result = doc_lexer_init(pState->manual.raw, c89str_len(pState->manual.raw), &docLexer);
if (result != 0) {
return ENOMEM;
}
for (;;) {
result = doc_lexer_next(&docLexer);
if (result != 0) {
break; /* We're done. */
}
if (docLexer.token == doc_token_type_header) {
manual_section section;
section.title = extract_manual_section_title(docLexer.pTokenStr, docLexer.tokenLen, §ion.level);
section.bookmark = extract_manual_section_bookmark(docLexer.pTokenStr, docLexer.tokenLen);
pState->manual.sections[pState->manual.sectionCount] = section;
pState->manual.sectionCount += 1;
}
}
}
return 0;
}
int load_examples(docstate* pState)
{
fs_result resultFS;
fs_iterator* pIterator;
/* Extract all examples. */
pIterator = fs_first(NULL, EXAMPLES_FOLDER, FS_READ);
while (pIterator != NULL) {
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. */
/* We need to get the contents of the file. */
doc_example example;
char* pFileData;
char filePath[4096];
fs_path_append(filePath, sizeof(filePath), EXAMPLES_FOLDER, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
/* Name. We don't care about the whole file path - just the name part of it. */
example.name = c89str_newn(NULL, pIterator->pName, pIterator->nameLen);
/* File content. */
resultFS = fs_file_open_and_read(NULL, filePath, FS_FORMAT_TEXT, (void**)&pFileData, NULL);
if (resultFS != FS_SUCCESS) {
printf("Failed to load example: %s\n", pIterator->pName);
return -1;
}
example.code = c89str_new(NULL, pFileData);
fs_free(pFileData, NULL);
pState->examples.examples[pState->examples.count] = example;
pState->examples.count += 1;
}
pIterator = fs_next(pIterator);
}
fs_free_iterator(pIterator);
return 0;
}
int load_api(c89str miniaudio, docstate* pState)
{
(void)miniaudio;
(void)pState;
return 0;
}
int load(docstate* pState)
{
int result;
c89str miniaudio;
C89STR_ZERO_OBJECT(pState);
/* Data is extracted from miniaudio.h, so we'll need to get that loaded as a start. */
miniaudio = load_file("miniaudio.h");
if (miniaudio == NULL) {
printf("Failed to load miniaudio.h");
return -1;
}
result = load_manual(miniaudio, pState);
if (result != 0) {
printf("Failed to load manual.");
return result;
}
result = load_examples(pState);
if (result != 0) {
printf("Failed to load examples.");
return result;
}
result = load_api(miniaudio, pState);
if (result != 0) {
printf("Failed to load API.");
return result;
}
/* We're done. */
c89str_delete(miniaudio, NULL);
return 0;
}
c89str raw_to_html(c89str raw);
c89str transform_inline_code(c89str html)
{
size_t offset = 0;
/*
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
with a monospace font.
*/
for (;;) {
size_t loc1;
size_t loc2;
c89str inner;
c89str replacement;
if (c89str_find(html + offset, "`", &loc1) != C89STR_SUCCESS) {
break; /* No more occurances. */
}
if (c89str_find(html + offset + loc1 + 1, "`", &loc2) != C89STR_SUCCESS) {
break; /* No more occurances. */
}
loc2 += loc1 + 1; /* Make the closing grave offset relative to "offset" like loc1. */
/* We need a copy of the section inside the graves just in case the replacement operation needs to reallocate the pointer. */
inner = c89str_newn(NULL, html + offset + loc1 + 1, loc2 - loc1 - 1);
if (inner == NULL) {
return NULL; /* Out of memory. */
}
replacement = c89str_newf(NULL, "<span style=\"font-family:monospace;\">%s</span>", inner);
c89str_delete(inner, NULL);
if (replacement == NULL) {
return NULL;
}
/* We now have what we need to replace the segment. */
html = c89str_replace(html, NULL, offset + loc1, loc2 - loc1 + 1, replacement, c89str_len(replacement));
if (html == NULL) {
c89str_delete(replacement, NULL);
return NULL; /* Out of memory. */
}
offset += loc1 + c89str_len(replacement);
c89str_delete(replacement, NULL);
}
return html;
}
c89str transform_urls_by_protocol(c89str html, const char* pProtocol)
{
size_t offset = 0;
size_t protocolLen;
protocolLen = c89str_strlen(pProtocol);
if (protocolLen == 0) {
return html;
}
for (;;) {
size_t loc1;
size_t loc2;
c89str url;
c89str replacement;
if (c89str_find(html + offset, pProtocol, &loc1) != C89STR_SUCCESS) {
break; /* No more occurances. */
}
loc2 = c89str_find_next_whitespace(html + offset + loc1, (size_t)-1, NULL);
if (loc2 != c89str_npos) {
loc2 += loc1;
} else {
loc2 = c89str_len(html) - offset;
}
/* We need a copy of the section inside the graves just in case the replacement operation needs to reallocate the pointer. */
url = c89str_trim(c89str_newn(NULL, html + offset + loc1, loc2 - loc1), NULL);
if (url == NULL) {
return NULL; /* Out of memory. */
}