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
|
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
|
+
+
+
+
+
+
+
+
+
+
|
** An instance of the following structure is passed through the
** "opaque" pointer.
*/
typedef struct MarkdownToHtml MarkdownToHtml;
struct MarkdownToHtml {
Blob *output_title; /* Store the title here */
bitfield64_t unique; /* Enables construction of unique #id elements */
#ifndef FOOTNOTES_WITHOUT_URI
Blob reqURI; /* REQUEST_URI with escaped quotes */
#endif
};
/* INTER_BLOCK -- skip a line between block level elements */
#define INTER_BLOCK(ob) \
do { if( blob_size(ob)>0 ) blob_append_char(ob, '\n'); } while (0)
/* BLOB_APPEND_LITERAL -- append a string literal to a blob */
#define BLOB_APPEND_LITERAL(blob, literal) \
blob_append((blob), "" literal, (sizeof literal)-1)
/*
* The empty string in the second argument leads to a syntax error
* when the macro is not used with a string literal. Unfortunately
* the error is not overly explicit.
*/
/* BLOB_APPEND_BLOB -- append blob contents to another */
#define BLOB_APPEND_BLOB(dest, src) \
blob_append((dest), blob_buffer(src), blob_size(src))
#ifndef FOOTNOTES_WITHOUT_URI
#define BLOB_APPEND_URI(dest,ctx) BLOB_APPEND_BLOB(dest,&((ctx)->reqURI))
#else
#define BLOB_APPEND_URI(dest,ctx)
#endif
/* Converts an integer to a null-terminated base26 representation
* Return empty string if that integer is negative. */
static bitfield64_t to_base26(int i, int uppercase){
bitfield64_t x;
int j;
x.u = 0;
|
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
|
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
|
-
-
+
+
+
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
+
-
+
-
-
+
+
+
-
-
+
+
+
|
sprintf(pos, "%s-%i-%s", ctx->unique.c, iMark, l.c);
if(span && blob_size(span)) {
BLOB_APPEND_LITERAL(ob,"<span class='notescope' id='noteref");
blob_appendf(ob,"%s'>",pos);
BLOB_APPEND_BLOB(ob, span);
blob_trim(ob);
BLOB_APPEND_LITERAL(ob,"<sup class='noteref'><a href='#footnote");
blob_appendf(ob,"%s'>%i</a></sup></span>", pos, iMark);
BLOB_APPEND_LITERAL(ob,"<sup class='noteref'><a href='");
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob,"#footnote%s'>%i</a></sup></span>", pos, iMark);
}else{
blob_trim(ob);
BLOB_APPEND_LITERAL(ob,"<sup class='noteref'><a href='#footnote");
blob_appendf(ob,"%s' id='noteref%s'>%i</a></sup>",
BLOB_APPEND_LITERAL(ob,"<sup class='noteref'><a href='");
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob,"#footnote%s' id='noteref%s'>%i</a></sup>",
pos, pos, iMark);
}
}else{ /* misreference */
assert( iMark == -1 );
sprintf(pos, "%s-%s", ctx->unique.c, l.c);
if(span && blob_size(span)) {
blob_appendf(ob, "<span class='notescope' id='misref%s'>", pos);
BLOB_APPEND_BLOB(ob, span);
blob_trim(ob);
BLOB_APPEND_LITERAL(ob,
"<sup class='noteref misref'><a href='#misreference");
blob_appendf(ob, "%s'>misref</a></sup></span>", pos);
BLOB_APPEND_LITERAL(ob, "<sup class='noteref misref'><a href='");
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob, "#misreference%s'>misref</a></sup></span>", pos);
}else{
blob_trim(ob);
BLOB_APPEND_LITERAL(ob,
"<sup class='noteref misref'><a href='#misreference");
blob_appendf(ob, "%s' id='misref%s'>", pos, pos);
BLOB_APPEND_LITERAL(ob, "<sup class='noteref misref'><a href='");
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob, "#misreference%s' id='misref%s'>", pos, pos);
BLOB_APPEND_LITERAL(ob, "misref</a></sup>");
}
}
return 1;
}
/* Render a single item of the footnotes list.
* Each backref gets a unique id to enable dynamic styling. */
static void html_footnote_item(
struct Blob *ob, const struct Blob *text, int iMark, int nUsed, void *opaque
){
const struct MarkdownToHtml* ctx = (struct MarkdownToHtml*)opaque;
const char * const unique = ((struct MarkdownToHtml*)opaque)->unique.c;
const char * const unique = ctx->unique.c;
assert( nUsed >= 0 );
/* expect BUGs if the following yields compiler warnings */
if( iMark < 0 ){ /* misreferences */
assert( iMark == -1 );
if( !nUsed ) return;
BLOB_APPEND_LITERAL(ob,"<li class='fn-misreference'>"
"<sup class='fn-backrefs'>");
if( nUsed == 1 ){
blob_appendf(ob,"<a id='misreference%s-a' "
"href='#misref%s-a'>^</a>", unique, unique);
blob_appendf(ob,"<a id='misreference%s-a' href='", unique);
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob,"#misref%s-a'>^</a>", unique);
}else{
int i;
blob_append_char(ob, '^');
for(i=0; i<nUsed && i<26; i++){
const int c = i + (unsigned)'a';
blob_appendf(ob," <a id='misreference%s-%c' "
"href='#misref%s-%c'>%c</a>", unique,c, unique,c, c);
blob_appendf(ob," <a id='misreference%s-%c' href='", unique,c);
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob,"#misref%s-%c'>%c</a>", unique,c, c);
}
if( i < nUsed ) BLOB_APPEND_LITERAL(ob," …");
}
BLOB_APPEND_LITERAL(ob,"</sup>\n<span>Misreference</span>");
}else if( nUsed ){ /* a regular footnote */
char pos[24];
|
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
|
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
|
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
|
}
memset(pos,0,24);
sprintf(pos, "%s-%i", unique, iMark);
blob_appendf(ob, "<li id='footnote%s' class='%s", pos, join);
if( nUsed == 1 ){
BLOB_APPEND_LITERAL(ob, "fn-monoref'><sup class='fn-backrefs'>");
blob_appendf(ob,"<a id='footnote%s-a' "
"href='#noteref%s-a'>^</a>", pos, pos);
blob_appendf(ob,"<a id='footnote%s-a' href='", pos);
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob,"#noteref%s-a'>^</a>", pos);
}else{
int i;
BLOB_APPEND_LITERAL(ob, "fn-polyref'><sup class='fn-backrefs'>^");
for(i=0; i<nUsed && i<26; i++){
const int c = i + (unsigned)'a';
blob_appendf(ob," <a id='footnote%s-%c'"
" href='#noteref%s-%c'>%c</a>", pos,c, pos,c, c);
blob_appendf(ob," <a id='footnote%s-%c' href='", pos,c);
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob,"#noteref%s-%c'>%c</a>", pos,c, c);
}
/* It's unlikely that so many backrefs will be usefull */
/* but maybe for some machine generated documents... */
for(; i<nUsed && i<676; i++){
const bitfield64_t l = to_base26(i,0);
blob_appendf(ob," <a id='footnote%s-%s'"
" href='#noteref%s-%s'>%s</a>",
blob_appendf(ob," <a id='footnote%s-%s' href='", pos, l.c);
BLOB_APPEND_URI(ob, ctx);
blob_appendf(ob,"#noteref%s-%s'>%s</a>", pos,l.c, l.c);
pos,l.c, pos,l.c, l.c);
}
if( i < nUsed ) BLOB_APPEND_LITERAL(ob," …");
}
BLOB_APPEND_LITERAL(ob,"</sup>\n");
if( join[0] ){
BLOB_APPEND_LITERAL(ob,"<sup class='fn-joined'></sup><ul>");
blob_append(ob,blob_buffer(text)+_jfi_sz,blob_size(text)-_jfi_sz);
|
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
|
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
|
+
+
+
+
+
|
html_normal_text,
/* misc. parameters */
"*_", /* emph_chars */
0 /* opaque */
};
static int invocation = -1; /* no marker for the first document */
static const char* zRU = 0; /* REQUEST_URI with escaped quotes */
MarkdownToHtml context;
memset(&context, 0, sizeof(context));
context.output_title = output_title;
context.unique = to_base26(invocation++,1);
if( !zRU ) zRU = escape_quotes(PD("REQUEST_URI",""));
#ifndef FOOTNOTES_WITHOUT_URI
blob_set( &context.reqURI, zRU );
#endif
html_renderer.opaque = &context;
if( output_title ) blob_reset(output_title);
blob_reset(output_body);
markdown(output_body, input_markdown, &html_renderer);
}
|