Fossil SCM

Minor code refactoring.

george 2022-02-04 19:47 markdown-footnotes
Commit 2636e2245e7ed14fb529cd4ae63526465984b9b763f26bb27d1b5543186905ef
1 file changed +26 -24
+26 -24
--- src/markdown.c
+++ src/markdown.c
@@ -171,13 +171,15 @@
171171
char_trigger active_char[256];
172172
int iDepth; /* Depth of recursion */
173173
int nBlobCache; /* Number of entries in aBlobCache */
174174
struct Blob *aBlobCache[20]; /* Cache of Blobs available for reuse */
175175
176
- struct Blob notes; /* array of footnotes */
177
- int nLabeled; /* number of footnotes found by the first pass */
178
- int iNotesCount; /* count distinct indices found in the second pass */
176
+ struct {
177
+ Blob all; /* array of footnotes */
178
+ int nLbled; /* number of labeled footnotes found during the first pass */
179
+ int nMarks; /* count distinct indices found in the second pass */
180
+ } notes;
179181
};
180182
181183
/* html_tag -- structure for quick HTML tag search (inspired from discount) */
182184
struct html_tag {
183185
const char *text;
@@ -1041,19 +1043,19 @@
10411043
size_t size
10421044
){
10431045
struct footnote *fn = NULL;
10441046
struct Blob *id = new_work_buffer(rndr);
10451047
if( build_ref_id(id, data, size)<0 ) goto cleanup;
1046
- fn = bsearch(id, blob_buffer(&rndr->notes),
1047
- rndr->nLabeled,
1048
+ fn = bsearch(id, blob_buffer(&rndr->notes.all),
1049
+ rndr->notes.nLbled,
10481050
sizeof (struct footnote),
10491051
cmp_link_ref);
10501052
if( !fn ) goto cleanup;
10511053
10521054
if( fn->index == 0 ){ /* the first reference to the footnote */
10531055
assert( fn->nUsed == 0 );
1054
- fn->index = ++(rndr->iNotesCount);
1056
+ fn->index = ++(rndr->notes.nMarks);
10551057
}
10561058
fn->nUsed++;
10571059
assert( fn->index > 0 );
10581060
assert( fn->nUsed > 0 );
10591061
cleanup:
@@ -1069,17 +1071,17 @@
10691071
size_t size
10701072
){
10711073
struct footnote fn = { empty_blob, empty_blob, 0, 0 };
10721074
while(size && (*text==' ' || *text=='\t')){ text++; size--; }
10731075
if(!size) return 0;
1074
- fn.index = ++(rndr->iNotesCount);
1076
+ fn.index = ++(rndr->notes.nMarks);
10751077
fn.nUsed = 1;
10761078
assert( fn.index > 0 );
10771079
blob_append(&fn.text, text, size);
1078
- blob_append(&rndr->notes, (char *)&fn, sizeof fn);
1079
- return (struct footnote*)( blob_buffer(&rndr->notes)
1080
- +( blob_size(&rndr->notes)-sizeof fn ));
1080
+ blob_append(&rndr->notes.all, (char *)&fn, sizeof fn);
1081
+ return (struct footnote*)( blob_buffer(&rndr->notes.all)
1082
+ +( blob_size(&rndr->notes.all)-sizeof fn ));
10811083
}
10821084
10831085
/* Return the offset of the matching closing bracket or 0 if not found.
10841086
* begin[0] must be either '[' or '(' */
10851087
static inline size_t matching_bracket_offset(
@@ -2453,12 +2455,12 @@
24532455
if( !rndrer ) return;
24542456
rndr.make = *rndrer;
24552457
rndr.nBlobCache = 0;
24562458
rndr.iDepth = 0;
24572459
rndr.refs = empty_blob;
2458
- rndr.notes = empty_blob;
2459
- rndr.iNotesCount = 0;
2460
+ rndr.notes.all = empty_blob;
2461
+ rndr.notes.nMarks = 0;
24602462
for(i=0; i<256; i++) rndr.active_char[i] = 0;
24612463
if( (rndr.make.emphasis
24622464
|| rndr.make.double_emphasis
24632465
|| rndr.make.triple_emphasis)
24642466
&& rndr.make.emph_chars
@@ -2480,11 +2482,11 @@
24802482
beg = 0;
24812483
for(const size_t size = blob_size(ib); beg<size ;){
24822484
const char* const data = blob_buffer(ib);
24832485
if( is_ref(data, beg, size, &end, &rndr.refs) ){
24842486
beg = end;
2485
- }else if(is_footnote(data, beg, size, &end, &rndr.notes)){
2487
+ }else if(is_footnote(data, beg, size, &end, &rndr.notes.all)){
24862488
/* FIXME: fossil_print("\nfootnote found at %i\n", beg); */
24872489
beg = end;
24882490
}else{ /* skipping to the next line */
24892491
end = beg;
24902492
while( end<size && data[end]!='\n' && data[end]!='\r' ){
@@ -2500,37 +2502,37 @@
25002502
end += 1;
25012503
}
25022504
beg = end;
25032505
}
25042506
}
2505
- assert( rndr.iNotesCount==0 );
2507
+ assert( rndr.notes.nMarks==0 );
25062508
/* sorting the reference array */
25072509
if( blob_size(&rndr.refs) ){
25082510
qsort(blob_buffer(&rndr.refs),
25092511
blob_size(&rndr.refs)/sizeof(struct link_ref),
25102512
sizeof(struct link_ref),
25112513
cmp_link_ref_sort);
25122514
}
2513
- rndr.nLabeled = COUNT_FOOTNOTES(&rndr.notes);
2515
+ rndr.notes.nLbled = COUNT_FOOTNOTES(&rndr.notes.all);
25142516
/* sorting the footnotes array by id */
2515
- if( rndr.nLabeled ){
2516
- qsort(blob_buffer(&rndr.notes), rndr.nLabeled, sizeof(struct footnote),
2517
- cmp_link_ref_sort);
2517
+ if( rndr.notes.nLbled ){
2518
+ qsort(blob_buffer(&rndr.notes.all), rndr.notes.nLbled,
2519
+ sizeof(struct footnote), cmp_link_ref_sort);
25182520
}
25192521
25202522
/* second pass: actual rendering */
25212523
if( rndr.make.prolog ) rndr.make.prolog(ob, rndr.make.opaque);
25222524
parse_block(ob, &rndr, blob_buffer(&text), blob_size(&text));
25232525
2524
- fn = (struct footnote*)blob_buffer(&rndr.notes);
2525
- if(rndr.iNotesCount && rndr.make.footnote_item && rndr.make.footnotes){
2526
+ fn = (struct footnote*)blob_buffer(&rndr.notes.all);
2527
+ if(rndr.notes.nMarks && rndr.make.footnote_item && rndr.make.footnotes){
25262528
Blob * one_item = new_work_buffer(&rndr);
25272529
Blob * all_items = new_work_buffer(&rndr);
2528
- qsort( fn, COUNT_FOOTNOTES(&rndr.notes), sizeof(struct footnote),
2530
+ qsort( fn, COUNT_FOOTNOTES(&rndr.notes.all), sizeof(struct footnote),
25292531
cmp_footnote_sort /* sort footnotes by index */ );
25302532
blob_reset( all_items );
2531
- for(i=0; i<rndr.iNotesCount; i++){
2533
+ for(i=0; i<rndr.notes.nMarks; i++){
25322534
assert( fn[i].index == i+1 );
25332535
25342536
blob_reset( one_item );
25352537
parse_inline( one_item, &rndr, blob_buffer(&fn[i].text),
25362538
blob_size(&fn[i].text));
@@ -2551,15 +2553,15 @@
25512553
blob_reset(&lr[i].id);
25522554
blob_reset(&lr[i].link);
25532555
blob_reset(&lr[i].title);
25542556
}
25552557
blob_reset(&rndr.refs);
2556
- end = COUNT_FOOTNOTES(&rndr.notes);
2558
+ end = COUNT_FOOTNOTES(&rndr.notes.all);
25572559
for(i=0; i<end; i++){
25582560
if(blob_size(&fn[i].id)) blob_reset(&fn[i].id);
25592561
blob_reset(&fn[i].text);
25602562
}
2561
- blob_reset(&rndr.notes);
2563
+ blob_reset(&rndr.notes.all);
25622564
for(i=0; i<rndr.nBlobCache; i++){
25632565
fossil_free(rndr.aBlobCache[i]);
25642566
}
25652567
}
25662568
--- src/markdown.c
+++ src/markdown.c
@@ -171,13 +171,15 @@
171 char_trigger active_char[256];
172 int iDepth; /* Depth of recursion */
173 int nBlobCache; /* Number of entries in aBlobCache */
174 struct Blob *aBlobCache[20]; /* Cache of Blobs available for reuse */
175
176 struct Blob notes; /* array of footnotes */
177 int nLabeled; /* number of footnotes found by the first pass */
178 int iNotesCount; /* count distinct indices found in the second pass */
 
 
179 };
180
181 /* html_tag -- structure for quick HTML tag search (inspired from discount) */
182 struct html_tag {
183 const char *text;
@@ -1041,19 +1043,19 @@
1041 size_t size
1042 ){
1043 struct footnote *fn = NULL;
1044 struct Blob *id = new_work_buffer(rndr);
1045 if( build_ref_id(id, data, size)<0 ) goto cleanup;
1046 fn = bsearch(id, blob_buffer(&rndr->notes),
1047 rndr->nLabeled,
1048 sizeof (struct footnote),
1049 cmp_link_ref);
1050 if( !fn ) goto cleanup;
1051
1052 if( fn->index == 0 ){ /* the first reference to the footnote */
1053 assert( fn->nUsed == 0 );
1054 fn->index = ++(rndr->iNotesCount);
1055 }
1056 fn->nUsed++;
1057 assert( fn->index > 0 );
1058 assert( fn->nUsed > 0 );
1059 cleanup:
@@ -1069,17 +1071,17 @@
1069 size_t size
1070 ){
1071 struct footnote fn = { empty_blob, empty_blob, 0, 0 };
1072 while(size && (*text==' ' || *text=='\t')){ text++; size--; }
1073 if(!size) return 0;
1074 fn.index = ++(rndr->iNotesCount);
1075 fn.nUsed = 1;
1076 assert( fn.index > 0 );
1077 blob_append(&fn.text, text, size);
1078 blob_append(&rndr->notes, (char *)&fn, sizeof fn);
1079 return (struct footnote*)( blob_buffer(&rndr->notes)
1080 +( blob_size(&rndr->notes)-sizeof fn ));
1081 }
1082
1083 /* Return the offset of the matching closing bracket or 0 if not found.
1084 * begin[0] must be either '[' or '(' */
1085 static inline size_t matching_bracket_offset(
@@ -2453,12 +2455,12 @@
2453 if( !rndrer ) return;
2454 rndr.make = *rndrer;
2455 rndr.nBlobCache = 0;
2456 rndr.iDepth = 0;
2457 rndr.refs = empty_blob;
2458 rndr.notes = empty_blob;
2459 rndr.iNotesCount = 0;
2460 for(i=0; i<256; i++) rndr.active_char[i] = 0;
2461 if( (rndr.make.emphasis
2462 || rndr.make.double_emphasis
2463 || rndr.make.triple_emphasis)
2464 && rndr.make.emph_chars
@@ -2480,11 +2482,11 @@
2480 beg = 0;
2481 for(const size_t size = blob_size(ib); beg<size ;){
2482 const char* const data = blob_buffer(ib);
2483 if( is_ref(data, beg, size, &end, &rndr.refs) ){
2484 beg = end;
2485 }else if(is_footnote(data, beg, size, &end, &rndr.notes)){
2486 /* FIXME: fossil_print("\nfootnote found at %i\n", beg); */
2487 beg = end;
2488 }else{ /* skipping to the next line */
2489 end = beg;
2490 while( end<size && data[end]!='\n' && data[end]!='\r' ){
@@ -2500,37 +2502,37 @@
2500 end += 1;
2501 }
2502 beg = end;
2503 }
2504 }
2505 assert( rndr.iNotesCount==0 );
2506 /* sorting the reference array */
2507 if( blob_size(&rndr.refs) ){
2508 qsort(blob_buffer(&rndr.refs),
2509 blob_size(&rndr.refs)/sizeof(struct link_ref),
2510 sizeof(struct link_ref),
2511 cmp_link_ref_sort);
2512 }
2513 rndr.nLabeled = COUNT_FOOTNOTES(&rndr.notes);
2514 /* sorting the footnotes array by id */
2515 if( rndr.nLabeled ){
2516 qsort(blob_buffer(&rndr.notes), rndr.nLabeled, sizeof(struct footnote),
2517 cmp_link_ref_sort);
2518 }
2519
2520 /* second pass: actual rendering */
2521 if( rndr.make.prolog ) rndr.make.prolog(ob, rndr.make.opaque);
2522 parse_block(ob, &rndr, blob_buffer(&text), blob_size(&text));
2523
2524 fn = (struct footnote*)blob_buffer(&rndr.notes);
2525 if(rndr.iNotesCount && rndr.make.footnote_item && rndr.make.footnotes){
2526 Blob * one_item = new_work_buffer(&rndr);
2527 Blob * all_items = new_work_buffer(&rndr);
2528 qsort( fn, COUNT_FOOTNOTES(&rndr.notes), sizeof(struct footnote),
2529 cmp_footnote_sort /* sort footnotes by index */ );
2530 blob_reset( all_items );
2531 for(i=0; i<rndr.iNotesCount; i++){
2532 assert( fn[i].index == i+1 );
2533
2534 blob_reset( one_item );
2535 parse_inline( one_item, &rndr, blob_buffer(&fn[i].text),
2536 blob_size(&fn[i].text));
@@ -2551,15 +2553,15 @@
2551 blob_reset(&lr[i].id);
2552 blob_reset(&lr[i].link);
2553 blob_reset(&lr[i].title);
2554 }
2555 blob_reset(&rndr.refs);
2556 end = COUNT_FOOTNOTES(&rndr.notes);
2557 for(i=0; i<end; i++){
2558 if(blob_size(&fn[i].id)) blob_reset(&fn[i].id);
2559 blob_reset(&fn[i].text);
2560 }
2561 blob_reset(&rndr.notes);
2562 for(i=0; i<rndr.nBlobCache; i++){
2563 fossil_free(rndr.aBlobCache[i]);
2564 }
2565 }
2566
--- src/markdown.c
+++ src/markdown.c
@@ -171,13 +171,15 @@
171 char_trigger active_char[256];
172 int iDepth; /* Depth of recursion */
173 int nBlobCache; /* Number of entries in aBlobCache */
174 struct Blob *aBlobCache[20]; /* Cache of Blobs available for reuse */
175
176 struct {
177 Blob all; /* array of footnotes */
178 int nLbled; /* number of labeled footnotes found during the first pass */
179 int nMarks; /* count distinct indices found in the second pass */
180 } notes;
181 };
182
183 /* html_tag -- structure for quick HTML tag search (inspired from discount) */
184 struct html_tag {
185 const char *text;
@@ -1041,19 +1043,19 @@
1043 size_t size
1044 ){
1045 struct footnote *fn = NULL;
1046 struct Blob *id = new_work_buffer(rndr);
1047 if( build_ref_id(id, data, size)<0 ) goto cleanup;
1048 fn = bsearch(id, blob_buffer(&rndr->notes.all),
1049 rndr->notes.nLbled,
1050 sizeof (struct footnote),
1051 cmp_link_ref);
1052 if( !fn ) goto cleanup;
1053
1054 if( fn->index == 0 ){ /* the first reference to the footnote */
1055 assert( fn->nUsed == 0 );
1056 fn->index = ++(rndr->notes.nMarks);
1057 }
1058 fn->nUsed++;
1059 assert( fn->index > 0 );
1060 assert( fn->nUsed > 0 );
1061 cleanup:
@@ -1069,17 +1071,17 @@
1071 size_t size
1072 ){
1073 struct footnote fn = { empty_blob, empty_blob, 0, 0 };
1074 while(size && (*text==' ' || *text=='\t')){ text++; size--; }
1075 if(!size) return 0;
1076 fn.index = ++(rndr->notes.nMarks);
1077 fn.nUsed = 1;
1078 assert( fn.index > 0 );
1079 blob_append(&fn.text, text, size);
1080 blob_append(&rndr->notes.all, (char *)&fn, sizeof fn);
1081 return (struct footnote*)( blob_buffer(&rndr->notes.all)
1082 +( blob_size(&rndr->notes.all)-sizeof fn ));
1083 }
1084
1085 /* Return the offset of the matching closing bracket or 0 if not found.
1086 * begin[0] must be either '[' or '(' */
1087 static inline size_t matching_bracket_offset(
@@ -2453,12 +2455,12 @@
2455 if( !rndrer ) return;
2456 rndr.make = *rndrer;
2457 rndr.nBlobCache = 0;
2458 rndr.iDepth = 0;
2459 rndr.refs = empty_blob;
2460 rndr.notes.all = empty_blob;
2461 rndr.notes.nMarks = 0;
2462 for(i=0; i<256; i++) rndr.active_char[i] = 0;
2463 if( (rndr.make.emphasis
2464 || rndr.make.double_emphasis
2465 || rndr.make.triple_emphasis)
2466 && rndr.make.emph_chars
@@ -2480,11 +2482,11 @@
2482 beg = 0;
2483 for(const size_t size = blob_size(ib); beg<size ;){
2484 const char* const data = blob_buffer(ib);
2485 if( is_ref(data, beg, size, &end, &rndr.refs) ){
2486 beg = end;
2487 }else if(is_footnote(data, beg, size, &end, &rndr.notes.all)){
2488 /* FIXME: fossil_print("\nfootnote found at %i\n", beg); */
2489 beg = end;
2490 }else{ /* skipping to the next line */
2491 end = beg;
2492 while( end<size && data[end]!='\n' && data[end]!='\r' ){
@@ -2500,37 +2502,37 @@
2502 end += 1;
2503 }
2504 beg = end;
2505 }
2506 }
2507 assert( rndr.notes.nMarks==0 );
2508 /* sorting the reference array */
2509 if( blob_size(&rndr.refs) ){
2510 qsort(blob_buffer(&rndr.refs),
2511 blob_size(&rndr.refs)/sizeof(struct link_ref),
2512 sizeof(struct link_ref),
2513 cmp_link_ref_sort);
2514 }
2515 rndr.notes.nLbled = COUNT_FOOTNOTES(&rndr.notes.all);
2516 /* sorting the footnotes array by id */
2517 if( rndr.notes.nLbled ){
2518 qsort(blob_buffer(&rndr.notes.all), rndr.notes.nLbled,
2519 sizeof(struct footnote), cmp_link_ref_sort);
2520 }
2521
2522 /* second pass: actual rendering */
2523 if( rndr.make.prolog ) rndr.make.prolog(ob, rndr.make.opaque);
2524 parse_block(ob, &rndr, blob_buffer(&text), blob_size(&text));
2525
2526 fn = (struct footnote*)blob_buffer(&rndr.notes.all);
2527 if(rndr.notes.nMarks && rndr.make.footnote_item && rndr.make.footnotes){
2528 Blob * one_item = new_work_buffer(&rndr);
2529 Blob * all_items = new_work_buffer(&rndr);
2530 qsort( fn, COUNT_FOOTNOTES(&rndr.notes.all), sizeof(struct footnote),
2531 cmp_footnote_sort /* sort footnotes by index */ );
2532 blob_reset( all_items );
2533 for(i=0; i<rndr.notes.nMarks; i++){
2534 assert( fn[i].index == i+1 );
2535
2536 blob_reset( one_item );
2537 parse_inline( one_item, &rndr, blob_buffer(&fn[i].text),
2538 blob_size(&fn[i].text));
@@ -2551,15 +2553,15 @@
2553 blob_reset(&lr[i].id);
2554 blob_reset(&lr[i].link);
2555 blob_reset(&lr[i].title);
2556 }
2557 blob_reset(&rndr.refs);
2558 end = COUNT_FOOTNOTES(&rndr.notes.all);
2559 for(i=0; i<end; i++){
2560 if(blob_size(&fn[i].id)) blob_reset(&fn[i].id);
2561 blob_reset(&fn[i].text);
2562 }
2563 blob_reset(&rndr.notes.all);
2564 for(i=0; i<rndr.nBlobCache; i++){
2565 fossil_free(rndr.aBlobCache[i]);
2566 }
2567 }
2568

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button