| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <string.h> |
| 9 |
#include <stdarg.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <time.h> |
| 12 |
#include <ctype.h> |
| 13 |
|
| 14 |
#include "config.h" |
| 15 |
|
| 16 |
#include "cstring.h" |
| 17 |
#include "markdown.h" |
| 18 |
#include "amalloc.h" |
| 19 |
|
| 20 |
typedef int (*stfu)(const void*,const void*); |
| 21 |
typedef void (*spanhandler)(MMIOT*,int); |
| 22 |
|
| 23 |
|
| 24 |
static void text(MMIOT *f); |
| 25 |
static Paragraph *display(Paragraph*, MMIOT*); |
| 26 |
|
| 27 |
|
| 28 |
int __mkd_footsort(Footnote *, Footnote *); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static void |
| 34 |
push(char *bfr, int size, MMIOT *f) |
| 35 |
{ |
| 36 |
while ( size-- > 0 ) |
| 37 |
EXPAND(f->in) = *bfr++; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static void |
| 45 |
pushc(char c, MMIOT *f) |
| 46 |
{ |
| 47 |
EXPAND(f->in) = c; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static inline int |
| 54 |
peek(MMIOT *f, int i) |
| 55 |
{ |
| 56 |
|
| 57 |
i += (f->isp-1); |
| 58 |
|
| 59 |
return (i >= 0) && (i < S(f->in)) ? (unsigned char)T(f->in)[i] : EOF; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static inline unsigned int |
| 66 |
pull(MMIOT *f) |
| 67 |
{ |
| 68 |
return ( f->isp < S(f->in) ) ? (unsigned char)T(f->in)[f->isp++] : EOF; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static inline char* |
| 75 |
cursor(MMIOT *f) |
| 76 |
{ |
| 77 |
return T(f->in) + f->isp; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
static inline int |
| 82 |
isthisspace(MMIOT *f, int i) |
| 83 |
{ |
| 84 |
int c = peek(f, i); |
| 85 |
|
| 86 |
if ( c == EOF ) |
| 87 |
return 1; |
| 88 |
if ( c & 0x80 ) |
| 89 |
return 0; |
| 90 |
return isspace(c) || (c < ' '); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static inline int |
| 95 |
isthisalnum(MMIOT *f, int i) |
| 96 |
{ |
| 97 |
int c = peek(f, i); |
| 98 |
|
| 99 |
return (c != EOF) && isalnum(c); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
static inline int |
| 104 |
isthisnonword(MMIOT *f, int i) |
| 105 |
{ |
| 106 |
return isthisspace(f, i) || ispunct(peek(f,i)); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
#define mmiotseek(f,x) ((f->isp = x), (f->last = 0)) |
| 115 |
#define mmiottell(f) (f->isp) |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
static void |
| 121 |
shift(MMIOT *f, int i) |
| 122 |
{ |
| 123 |
if (f->isp + i >= 0 ) |
| 124 |
f->isp += i; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static void |
| 131 |
Qchar(int c, MMIOT *f) |
| 132 |
{ |
| 133 |
block *cur; |
| 134 |
|
| 135 |
if ( S(f->Q) == 0 ) { |
| 136 |
cur = &EXPAND(f->Q); |
| 137 |
memset(cur, 0, sizeof *cur); |
| 138 |
cur->b_type = bTEXT; |
| 139 |
} |
| 140 |
else |
| 141 |
cur = &T(f->Q)[S(f->Q)-1]; |
| 142 |
|
| 143 |
EXPAND(cur->b_text) = c; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static void |
| 151 |
Qstring(char *s, MMIOT *f) |
| 152 |
{ |
| 153 |
while (*s) |
| 154 |
Qchar(*s++, f); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static void |
| 161 |
Qwrite(char *s, int size, MMIOT *f) |
| 162 |
{ |
| 163 |
while (size-- > 0) |
| 164 |
Qchar(*s++, f); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
static void |
| 171 |
Qprintf(MMIOT *f, char *fmt, ...) |
| 172 |
{ |
| 173 |
char bfr[80]; |
| 174 |
va_list ptr; |
| 175 |
|
| 176 |
va_start(ptr,fmt); |
| 177 |
vsnprintf(bfr, sizeof bfr, fmt, ptr); |
| 178 |
va_end(ptr); |
| 179 |
Qstring(bfr, f); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
static int Qchar_mkd_sta_function_t(const int n, const void* iot) { |
| 184 |
Qchar(n, iot); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
static void |
| 191 |
Qanchor(struct line *p, MMIOT *f) |
| 192 |
{ |
| 193 |
mkd_string_to_anchor(T(p->text), S(p->text), |
| 194 |
Qchar_mkd_sta_function_t, f, 1, f); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
static void |
| 201 |
Qem(MMIOT *f, char c, int count) |
| 202 |
{ |
| 203 |
block *p = &EXPAND(f->Q); |
| 204 |
|
| 205 |
memset(p, 0, sizeof *p); |
| 206 |
p->b_type = (c == '*') ? bSTAR : bUNDER; |
| 207 |
p->b_char = c; |
| 208 |
p->b_count = count; |
| 209 |
|
| 210 |
memset(&EXPAND(f->Q), 0, sizeof(block)); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
void |
| 217 |
___mkd_reparse(char *bfr, int size, mkd_flag_t flags, MMIOT *f, char *esc) |
| 218 |
{ |
| 219 |
MMIOT sub; |
| 220 |
struct escaped e; |
| 221 |
|
| 222 |
___mkd_initmmiot(&sub, f->footnotes); |
| 223 |
|
| 224 |
sub.flags = f->flags | flags; |
| 225 |
sub.cb = f->cb; |
| 226 |
sub.ref_prefix = f->ref_prefix; |
| 227 |
|
| 228 |
if ( esc ) { |
| 229 |
sub.esc = &e; |
| 230 |
e.up = f->esc; |
| 231 |
e.text = esc; |
| 232 |
} |
| 233 |
else |
| 234 |
sub.esc = f->esc; |
| 235 |
|
| 236 |
push(bfr, size, &sub); |
| 237 |
pushc(0, &sub); |
| 238 |
S(sub.in)--; |
| 239 |
|
| 240 |
text(&sub); |
| 241 |
___mkd_emblock(&sub); |
| 242 |
|
| 243 |
Qwrite(T(sub.out), S(sub.out), f); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
f->last = sub.last; |
| 249 |
|
| 250 |
___mkd_freemmiot(&sub, f->footnotes); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
static int |
| 258 |
escaped(MMIOT *f, char c) |
| 259 |
{ |
| 260 |
struct escaped *thing = f->esc; |
| 261 |
|
| 262 |
while ( thing ) { |
| 263 |
if ( strchr(thing->text, c) ) |
| 264 |
return 1; |
| 265 |
thing = thing->up; |
| 266 |
} |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
static void |
| 275 |
puturl(char *s, int size, MMIOT *f, int display) |
| 276 |
{ |
| 277 |
unsigned char c; |
| 278 |
|
| 279 |
while ( size-- > 0 ) { |
| 280 |
c = *s++; |
| 281 |
|
| 282 |
if ( c == '\\' && size-- > 0 ) { |
| 283 |
c = *s++; |
| 284 |
|
| 285 |
if ( !( ispunct(c) || isspace(c) ) ) |
| 286 |
Qchar('\\', f); |
| 287 |
} |
| 288 |
|
| 289 |
if ( c == '&' ) |
| 290 |
Qstring("&", f); |
| 291 |
else if ( c == '<' ) |
| 292 |
Qstring("<", f); |
| 293 |
else if ( c == '"' ) |
| 294 |
Qstring("%22", f); |
| 295 |
else if ( isalnum(c) || ispunct(c) || (display && isspace(c)) ) |
| 296 |
Qchar(c, f); |
| 297 |
else if ( c == MKD_EOLN ) |
| 298 |
Qstring(" ", f); |
| 299 |
else |
| 300 |
Qprintf(f, "%%%02X", c); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
static int |
| 308 |
eatspace(MMIOT *f) |
| 309 |
{ |
| 310 |
int c; |
| 311 |
|
| 312 |
for ( ; ((c=peek(f, 1)) != EOF) && isspace(c); pull(f) ) |
| 313 |
; |
| 314 |
return c; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
static int |
| 321 |
parenthetical(int in, int out, MMIOT *f) |
| 322 |
{ |
| 323 |
int size, indent, c; |
| 324 |
|
| 325 |
for ( indent=1,size=0; indent; size++ ) { |
| 326 |
if ( (c = pull(f)) == EOF ) |
| 327 |
return EOF; |
| 328 |
else if ( (c == '\\') && (peek(f,1) == out || peek(f,1) == in) ) { |
| 329 |
++size; |
| 330 |
pull(f); |
| 331 |
} |
| 332 |
else if ( c == in ) |
| 333 |
++indent; |
| 334 |
else if ( c == out ) |
| 335 |
--indent; |
| 336 |
} |
| 337 |
return size ? (size-1) : 0; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
static int |
| 344 |
linkylabel(MMIOT *f, Cstring *res) |
| 345 |
{ |
| 346 |
char *ptr = cursor(f); |
| 347 |
int size; |
| 348 |
|
| 349 |
if ( (size = parenthetical('[',']',f)) != EOF ) { |
| 350 |
T(*res) = ptr; |
| 351 |
S(*res) = size; |
| 352 |
return 1; |
| 353 |
} |
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static int |
| 361 |
linkytitle(MMIOT *f, char quote, Footnote *ref) |
| 362 |
{ |
| 363 |
int whence = mmiottell(f); |
| 364 |
char *title = cursor(f); |
| 365 |
char *e; |
| 366 |
register int c; |
| 367 |
|
| 368 |
while ( (c = pull(f)) != EOF ) { |
| 369 |
e = cursor(f); |
| 370 |
if ( c == quote ) { |
| 371 |
if ( (c = eatspace(f)) == ')' ) { |
| 372 |
T(ref->title) = 1+title; |
| 373 |
S(ref->title) = (e-title)-2; |
| 374 |
return 1; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
mmiotseek(f, whence); |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
static int |
| 386 |
linkysize(MMIOT *f, Footnote *ref) |
| 387 |
{ |
| 388 |
int height=0, width=0; |
| 389 |
int whence = mmiottell(f); |
| 390 |
int c; |
| 391 |
|
| 392 |
if ( isspace(peek(f,0)) ) { |
| 393 |
pull(f); |
| 394 |
|
| 395 |
for ( c = pull(f); isdigit(c); c = pull(f)) |
| 396 |
width = (width * 10) + (c - '0'); |
| 397 |
|
| 398 |
if ( c == 'x' ) { |
| 399 |
for ( c = pull(f); isdigit(c); c = pull(f)) |
| 400 |
height = (height*10) + (c - '0'); |
| 401 |
|
| 402 |
if ( isspace(c) ) |
| 403 |
c = eatspace(f); |
| 404 |
|
| 405 |
if ( (c == ')') || ((c == '\'' || c == '"') && linkytitle(f, c, ref)) ) { |
| 406 |
ref->height = height; |
| 407 |
ref->width = width; |
| 408 |
return 1; |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
mmiotseek(f, whence); |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
static int |
| 425 |
linkybroket(MMIOT *f, int image, Footnote *p) |
| 426 |
{ |
| 427 |
int c; |
| 428 |
int good = 0; |
| 429 |
|
| 430 |
T(p->link) = cursor(f); |
| 431 |
for ( S(p->link)=0; (c = pull(f)) != '>'; ++S(p->link) ) { |
| 432 |
|
| 433 |
|
| 434 |
if ( c == EOF ) |
| 435 |
return 0; |
| 436 |
else if ( (c == '\\') && ispunct(peek(f,2)) ) { |
| 437 |
++S(p->link); |
| 438 |
pull(f); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
c = eatspace(f); |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
if ( ( c == '\'' || c == '"' ) && linkytitle(f,c,p) ) |
| 447 |
good=1; |
| 448 |
else if ( image && (c == '=') && linkysize(f,p) ) |
| 449 |
good=1; |
| 450 |
else |
| 451 |
good=( c == ')' ); |
| 452 |
|
| 453 |
if ( good ) { |
| 454 |
if ( peek(f, 1) == ')' ) |
| 455 |
pull(f); |
| 456 |
|
| 457 |
___mkd_tidy(&p->link); |
| 458 |
} |
| 459 |
|
| 460 |
return good; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
static int |
| 471 |
linkyurl(MMIOT *f, int image, Footnote *p) |
| 472 |
{ |
| 473 |
int c; |
| 474 |
int mayneedtotrim=0; |
| 475 |
|
| 476 |
if ( (c = eatspace(f)) == EOF ) |
| 477 |
return 0; |
| 478 |
|
| 479 |
if ( c == '<' ) { |
| 480 |
pull(f); |
| 481 |
if ( !is_flag_set(f->flags, MKD_1_COMPAT) ) |
| 482 |
return linkybroket(f,image,p); |
| 483 |
mayneedtotrim=1; |
| 484 |
} |
| 485 |
|
| 486 |
T(p->link) = cursor(f); |
| 487 |
for ( S(p->link)=0; (c = peek(f,1)) != ')'; ++S(p->link) ) { |
| 488 |
if ( c == EOF ) |
| 489 |
return 0; |
| 490 |
else if ( (c == '"' || c == '\'') && linkytitle(f, c, p) ) |
| 491 |
break; |
| 492 |
else if ( image && (c == '=') && linkysize(f, p) ) |
| 493 |
break; |
| 494 |
else if ( (c == '\\') && ispunct(peek(f,2)) ) { |
| 495 |
++S(p->link); |
| 496 |
pull(f); |
| 497 |
} |
| 498 |
pull(f); |
| 499 |
} |
| 500 |
if ( peek(f, 1) == ')' ) |
| 501 |
pull(f); |
| 502 |
|
| 503 |
___mkd_tidy(&p->link); |
| 504 |
|
| 505 |
if ( mayneedtotrim && (T(p->link)[S(p->link)-1] == '>') ) |
| 506 |
--S(p->link); |
| 507 |
|
| 508 |
return 1; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
static struct _protocol { |
| 516 |
char *name; |
| 517 |
int nlen; |
| 518 |
} protocol[] = { |
| 519 |
#define _aprotocol(x) { x, (sizeof x)-1 } |
| 520 |
_aprotocol( "https:" ), |
| 521 |
_aprotocol( "http:" ), |
| 522 |
_aprotocol( "news:" ), |
| 523 |
_aprotocol( "ftp:" ), |
| 524 |
#undef _aprotocol |
| 525 |
}; |
| 526 |
#define NRPROTOCOLS (sizeof protocol / sizeof protocol[0]) |
| 527 |
|
| 528 |
|
| 529 |
static int |
| 530 |
isautoprefix(char *text, int size) |
| 531 |
{ |
| 532 |
int i; |
| 533 |
struct _protocol *p; |
| 534 |
|
| 535 |
for (i=0, p=protocol; i < NRPROTOCOLS; i++, p++) |
| 536 |
if ( (size >= p->nlen) && strncasecmp(text, p->name, p->nlen) == 0 ) |
| 537 |
return 1; |
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
typedef struct linkytype { |
| 547 |
char *pat; |
| 548 |
int szpat; |
| 549 |
char *link_pfx; |
| 550 |
char *link_sfx; |
| 551 |
int WxH; |
| 552 |
char *text_pfx; |
| 553 |
char *text_sfx; |
| 554 |
int flags; |
| 555 |
int kind; |
| 556 |
#define IS_URL 0x01 |
| 557 |
} linkytype; |
| 558 |
|
| 559 |
static linkytype imaget = { 0, 0, "<img src=\"", "\"", |
| 560 |
1, " alt=\"", "\" />", MKD_NOIMAGE|MKD_TAGTEXT, IS_URL }; |
| 561 |
static linkytype linkt = { 0, 0, "<a href=\"", "\"", |
| 562 |
0, ">", "</a>", MKD_NOLINKS, IS_URL }; |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
static linkytype specials[] = { |
| 572 |
{ "id:", 3, "<span id=\"", "\"", 0, ">", "</span>", 0, 0 }, |
| 573 |
{ "raw:", 4, 0, 0, 0, 0, 0, MKD_NOHTML, 0 }, |
| 574 |
{ "lang:", 5, "<span lang=\"", "\"", 0, ">", "</span>", 0, 0 }, |
| 575 |
{ "abbr:", 5, "<abbr title=\"", "\"", 0, ">", "</abbr>", 0, 0 }, |
| 576 |
{ "class:", 6, "<span class=\"", "\"", 0, ">", "</span>", 0, 0 }, |
| 577 |
} ; |
| 578 |
|
| 579 |
#define NR(x) (sizeof x / sizeof x[0]) |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
static linkytype * |
| 584 |
pseudo(Cstring t) |
| 585 |
{ |
| 586 |
int i; |
| 587 |
linkytype *r; |
| 588 |
|
| 589 |
for ( i=0, r=specials; i < NR(specials); i++,r++ ) { |
| 590 |
if ( (S(t) > r->szpat) && (strncasecmp(T(t), r->pat, r->szpat) == 0) ) |
| 591 |
return r; |
| 592 |
} |
| 593 |
return 0; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
static void |
| 600 |
printlinkyref(MMIOT *f, linkytype *tag, char *link, int size) |
| 601 |
{ |
| 602 |
char *edit; |
| 603 |
|
| 604 |
if ( is_flag_set(f->flags, IS_LABEL) ) |
| 605 |
return; |
| 606 |
|
| 607 |
Qstring(tag->link_pfx, f); |
| 608 |
|
| 609 |
if ( tag->kind & IS_URL ) { |
| 610 |
if ( f->cb && f->cb->e_url && (edit = (*f->cb->e_url)(link, size, f->cb->e_data)) ) { |
| 611 |
puturl(edit, strlen(edit), f, 0); |
| 612 |
if ( f->cb->e_free ) (*f->cb->e_free)(edit, f->cb->e_data); |
| 613 |
} |
| 614 |
else |
| 615 |
puturl(link + tag->szpat, size - tag->szpat, f, 0); |
| 616 |
} |
| 617 |
else |
| 618 |
___mkd_reparse(link + tag->szpat, size - tag->szpat, MKD_TAGTEXT, f, 0); |
| 619 |
|
| 620 |
Qstring(tag->link_sfx, f); |
| 621 |
|
| 622 |
if ( f->cb && f->cb->e_flags && (edit = (*f->cb->e_flags)(link, size, f->cb->e_data)) ) { |
| 623 |
Qchar(' ', f); |
| 624 |
Qstring(edit, f); |
| 625 |
if ( f->cb->e_free ) (*f->cb->e_free)(edit, f->cb->e_data); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
static char * |
| 634 |
p_or_nothing(MMIOT *p) |
| 635 |
{ |
| 636 |
return p->ref_prefix ? p->ref_prefix : "fn"; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
static int |
| 643 |
extra_linky(MMIOT *f, Cstring text, Footnote *ref) |
| 644 |
{ |
| 645 |
if ( ref->flags & REFERENCED ) |
| 646 |
return 0; |
| 647 |
|
| 648 |
if ( f->flags & IS_LABEL ) |
| 649 |
___mkd_reparse(T(text), S(text), linkt.flags, f, 0); |
| 650 |
else { |
| 651 |
ref->flags |= REFERENCED; |
| 652 |
ref->refnumber = ++ f->footnotes->reference; |
| 653 |
Qprintf(f, "<sup id=\"%sref:%d\"><a href=\"#%s:%d\" rel=\"footnote\">%d</a></sup>", |
| 654 |
p_or_nothing(f), ref->refnumber, |
| 655 |
p_or_nothing(f), ref->refnumber, ref->refnumber); |
| 656 |
} |
| 657 |
return 1; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
static int |
| 666 |
safelink(Cstring link) |
| 667 |
{ |
| 668 |
char *p, *colon; |
| 669 |
|
| 670 |
if ( T(link) == 0 ) |
| 671 |
return 1; |
| 672 |
|
| 673 |
p = T(link); |
| 674 |
if ( (colon = memchr(p, ':', S(link))) == 0 ) |
| 675 |
return 1; |
| 676 |
|
| 677 |
if ( !isalpha(*p) ) |
| 678 |
return 1; |
| 679 |
while ( ++p < colon ) |
| 680 |
if ( !(isalnum(*p) || *p == '.' || *p == '+' || *p == '-') ) |
| 681 |
return 1; |
| 682 |
|
| 683 |
return isautoprefix(T(link), S(link)); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
static int |
| 690 |
linkyformat(MMIOT *f, Cstring text, int image, Footnote *ref) |
| 691 |
{ |
| 692 |
linkytype *tag; |
| 693 |
|
| 694 |
|
| 695 |
if ( image ) |
| 696 |
tag = &imaget; |
| 697 |
else if ( tag = pseudo(ref->link) ) { |
| 698 |
if ( is_flag_set(f->flags, MKD_NO_EXT) || is_flag_set(f->flags, MKD_SAFELINK) ) |
| 699 |
return 0; |
| 700 |
} |
| 701 |
else if ( is_flag_set(f->flags, MKD_SAFELINK) && !safelink(ref->link) ) |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
return 0; |
| 706 |
else |
| 707 |
tag = &linkt; |
| 708 |
|
| 709 |
if ( f->flags & tag->flags ) |
| 710 |
return 0; |
| 711 |
|
| 712 |
if ( is_flag_set(f->flags, IS_LABEL) ) |
| 713 |
___mkd_reparse(T(text), S(text), tag->flags, f, 0); |
| 714 |
else if ( tag->link_pfx ) { |
| 715 |
printlinkyref(f, tag, T(ref->link), S(ref->link)); |
| 716 |
|
| 717 |
if ( tag->WxH ) { |
| 718 |
if ( ref->height ) Qprintf(f," height=\"%d\"", ref->height); |
| 719 |
if ( ref->width ) Qprintf(f, " width=\"%d\"", ref->width); |
| 720 |
} |
| 721 |
|
| 722 |
if ( S(ref->title) ) { |
| 723 |
Qstring(" title=\"", f); |
| 724 |
___mkd_reparse(T(ref->title), S(ref->title), MKD_TAGTEXT, f, 0); |
| 725 |
Qchar('"', f); |
| 726 |
} |
| 727 |
|
| 728 |
Qstring(tag->text_pfx, f); |
| 729 |
___mkd_reparse(T(text), S(text), tag->flags, f, 0); |
| 730 |
Qstring(tag->text_sfx, f); |
| 731 |
} |
| 732 |
else |
| 733 |
Qwrite(T(ref->link) + tag->szpat, S(ref->link) - tag->szpat, f); |
| 734 |
|
| 735 |
return 1; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
static int |
| 743 |
linkylinky(int image, MMIOT *f) |
| 744 |
{ |
| 745 |
int start = mmiottell(f); |
| 746 |
Cstring name; |
| 747 |
Footnote key, *ref; |
| 748 |
|
| 749 |
int status = 0; |
| 750 |
int extra_footnote = 0; |
| 751 |
|
| 752 |
CREATE(name); |
| 753 |
memset(&key, 0, sizeof key); |
| 754 |
|
| 755 |
if ( linkylabel(f, &name) ) { |
| 756 |
if ( peek(f,1) == '(' ) { |
| 757 |
pull(f); |
| 758 |
if ( linkyurl(f, image, &key) ) |
| 759 |
status = linkyformat(f, name, image, &key); |
| 760 |
} |
| 761 |
else { |
| 762 |
int goodlink, implicit_mark = mmiottell(f); |
| 763 |
|
| 764 |
if ( is_flag_set(f->flags, MKD_EXTRA_FOOTNOTE) |
| 765 |
&& !is_flag_set(f->flags, MKD_STRICT) |
| 766 |
&& (!image) |
| 767 |
&& S(name) |
| 768 |
&& T(name)[0] == '^' ) { |
| 769 |
extra_footnote = 1; |
| 770 |
goodlink = 1; |
| 771 |
} |
| 772 |
else { |
| 773 |
if ( isspace(peek(f,1)) ) |
| 774 |
pull(f); |
| 775 |
|
| 776 |
if ( peek(f,1) == '[' ) { |
| 777 |
pull(f); |
| 778 |
goodlink = linkylabel(f, &key.tag); |
| 779 |
} |
| 780 |
else { |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
mmiotseek(f, implicit_mark); |
| 785 |
goodlink = !is_flag_set(f->flags, MKD_1_COMPAT); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
if ( goodlink ) { |
| 790 |
if ( !S(key.tag) ) { |
| 791 |
DELETE(key.tag); |
| 792 |
T(key.tag) = T(name); |
| 793 |
S(key.tag) = S(name); |
| 794 |
} |
| 795 |
|
| 796 |
if ( ref = bsearch(&key, T(f->footnotes->note), |
| 797 |
S(f->footnotes->note), |
| 798 |
sizeof key, (stfu)__mkd_footsort) ) { |
| 799 |
if ( extra_footnote ) |
| 800 |
status = extra_linky(f,name,ref); |
| 801 |
else |
| 802 |
status = linkyformat(f, name, image, ref); |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
DELETE(name); |
| 809 |
___mkd_freefootnote(&key); |
| 810 |
|
| 811 |
if ( status == 0 ) |
| 812 |
mmiotseek(f, start); |
| 813 |
|
| 814 |
return status; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
static void |
| 822 |
cputc(int c, MMIOT *f) |
| 823 |
{ |
| 824 |
switch (c) { |
| 825 |
case '&': Qstring("&", f); break; |
| 826 |
case '>': Qstring(">", f); break; |
| 827 |
case '<': Qstring("<", f); break; |
| 828 |
default : Qchar(c, f); break; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
static void |
| 837 |
mangle(char *s, int len, MMIOT *f) |
| 838 |
{ |
| 839 |
while ( len-- > 0 ) { |
| 840 |
#if DEBIAN_GLITCH |
| 841 |
Qprintf(f, "&#%02d;", *((unsigned char*)(s++)) ); |
| 842 |
#else |
| 843 |
Qstring("&#", f); |
| 844 |
Qprintf(f, COINTOSS() ? "x%02x;" : "%02d;", *((unsigned char*)(s++)) ); |
| 845 |
#endif |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
static int |
| 853 |
nrticks(int offset, int tickchar, MMIOT *f) |
| 854 |
{ |
| 855 |
int tick = 0; |
| 856 |
|
| 857 |
while ( peek(f, offset+tick) == tickchar ) tick++; |
| 858 |
|
| 859 |
return tick; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
static int |
| 870 |
matchticks(MMIOT *f, int tickchar, int ticks, int *endticks) |
| 871 |
{ |
| 872 |
int size, count, c; |
| 873 |
int subsize=0, subtick=0; |
| 874 |
|
| 875 |
*endticks = ticks; |
| 876 |
for (size = 0; (c=peek(f,size+ticks)) != EOF; size ++) { |
| 877 |
if ( (c == tickchar) && ( count = nrticks(size+ticks,tickchar,f)) ) { |
| 878 |
if ( count == ticks ) |
| 879 |
return size; |
| 880 |
else if ( count ) { |
| 881 |
if ( (count > subtick) && (count < ticks) ) { |
| 882 |
subsize = size; |
| 883 |
subtick = count; |
| 884 |
} |
| 885 |
size += count; |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
if ( subsize ) { |
| 890 |
*endticks = subtick; |
| 891 |
return subsize; |
| 892 |
} |
| 893 |
return 0; |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
static void |
| 902 |
code(MMIOT *f, char *s, int length) |
| 903 |
{ |
| 904 |
int i,c; |
| 905 |
|
| 906 |
for ( i=0; i < length; i++ ) |
| 907 |
if ( (c = s[i]) == MKD_EOLN) |
| 908 |
Qstring(" ", f); |
| 909 |
else if ( c == '\\' && (i < length-1) && escaped(f, s[i+1]) ) |
| 910 |
cputc(s[++i], f); |
| 911 |
else |
| 912 |
cputc(c, f); |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
static void |
| 918 |
delspan(MMIOT *f, int size) |
| 919 |
{ |
| 920 |
Qstring("<del>", f); |
| 921 |
___mkd_reparse(cursor(f)-1, size, 0, f, 0); |
| 922 |
Qstring("</del>", f); |
| 923 |
} |
| 924 |
|
| 925 |
#ifdef TYPORA |
| 926 |
|
| 927 |
|
| 928 |
static void |
| 929 |
subspan(MMIOT *f, int size) |
| 930 |
{ |
| 931 |
Qstring("<sub>", f); |
| 932 |
___mkd_reparse(cursor(f)-1, size, 0, f, 0); |
| 933 |
Qstring("</sub>", f); |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
static void |
| 940 |
supspan(MMIOT *f, int size) |
| 941 |
{ |
| 942 |
Qstring("<sup>", f); |
| 943 |
___mkd_reparse(cursor(f)-1, size, 0, f, 0); |
| 944 |
Qstring("</sup>", f); |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
static void |
| 951 |
highlightspan(MMIOT *f, int size) |
| 952 |
{ |
| 953 |
Qstring("<mark>", f); |
| 954 |
___mkd_reparse(cursor(f)-1, size, 0, f, 0); |
| 955 |
Qstring("</mark>", f); |
| 956 |
} |
| 957 |
#endif |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
static void |
| 965 |
codespan(MMIOT *f, int size) |
| 966 |
{ |
| 967 |
int i=0; |
| 968 |
|
| 969 |
if ( size > 1 && peek(f, size-1) == ' ' ) --size; |
| 970 |
if ( peek(f,i) == ' ' ) ++i, --size; |
| 971 |
|
| 972 |
Qstring("<code>", f); |
| 973 |
code(f, cursor(f)+(i-1), size); |
| 974 |
Qstring("</code>", f); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
static int |
| 982 |
forbidden_tag(MMIOT *f) |
| 983 |
{ |
| 984 |
int c = toupper(peek(f, 1)); |
| 985 |
|
| 986 |
if ( is_flag_set(f->flags, MKD_NOHTML) ) |
| 987 |
return 1; |
| 988 |
|
| 989 |
if ( c == 'A' && is_flag_set(f->flags, MKD_NOLINKS) && !isthisalnum(f,2) ) |
| 990 |
return 1; |
| 991 |
if ( c == 'I' && is_flag_set(f->flags, MKD_NOIMAGE) |
| 992 |
&& strncasecmp(cursor(f)+1, "MG", 2) == 0 |
| 993 |
&& !isthisalnum(f,4) ) |
| 994 |
return 1; |
| 995 |
return 0; |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
static int |
| 1005 |
maybe_address(char *p, int size) |
| 1006 |
{ |
| 1007 |
int ok = 0; |
| 1008 |
|
| 1009 |
for ( ;size && (isalnum(*p) || strchr("._-+*", *p)); ++p, --size) |
| 1010 |
; |
| 1011 |
|
| 1012 |
if ( ! (size && *p == '@') ) |
| 1013 |
return 0; |
| 1014 |
|
| 1015 |
--size, ++p; |
| 1016 |
|
| 1017 |
if ( size && *p == '.' ) return 0; |
| 1018 |
|
| 1019 |
for ( ;size && (isalnum(*p) || strchr("._-+", *p)); ++p, --size ) |
| 1020 |
if ( *p == '.' && size > 1 ) ok = 1; |
| 1021 |
|
| 1022 |
return size ? 0 : ok; |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
static int |
| 1032 |
process_possible_link(MMIOT *f, int size) |
| 1033 |
{ |
| 1034 |
int address= 0; |
| 1035 |
int mailto = 0; |
| 1036 |
char *text = cursor(f); |
| 1037 |
|
| 1038 |
if ( is_flag_set(f->flags, MKD_NOLINKS) ) return 0; |
| 1039 |
|
| 1040 |
if ( (size > 7) && strncasecmp(text, "mailto:", 7) == 0 ) { |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
address = 1; |
| 1045 |
mailto = 7; |
| 1046 |
} |
| 1047 |
else |
| 1048 |
address = maybe_address(text, size); |
| 1049 |
|
| 1050 |
if ( address ) { |
| 1051 |
Qstring("<a href=\"", f); |
| 1052 |
if ( !mailto ) { |
| 1053 |
|
| 1054 |
mangle("mailto:", 7, f); |
| 1055 |
} |
| 1056 |
mangle(text, size, f); |
| 1057 |
Qstring("\">", f); |
| 1058 |
mangle(text+mailto, size-mailto, f); |
| 1059 |
Qstring("</a>", f); |
| 1060 |
return 1; |
| 1061 |
} |
| 1062 |
else if ( isautoprefix(text, size) ) { |
| 1063 |
printlinkyref(f, &linkt, text, size); |
| 1064 |
Qchar('>', f); |
| 1065 |
puturl(text,size,f, 1); |
| 1066 |
Qstring("</a>", f); |
| 1067 |
return 1; |
| 1068 |
} |
| 1069 |
return 0; |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
static inline int |
| 1078 |
is_a_strict_tag_prefix(int c) |
| 1079 |
{ |
| 1080 |
return isalpha(c) || (c == '/') || (c == '!') || (c == '$') || (c == '?'); |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
static int |
| 1091 |
maybe_tag_or_link(MMIOT *f) |
| 1092 |
{ |
| 1093 |
int c, size=0; |
| 1094 |
|
| 1095 |
if ( is_flag_set(f->flags, MKD_TAGTEXT) ) |
| 1096 |
return 0; |
| 1097 |
|
| 1098 |
c = peek(f, 1); |
| 1099 |
|
| 1100 |
|
| 1101 |
if ( is_a_strict_tag_prefix(c) ) { |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
size=1; |
| 1107 |
while ( (c=peek(f,size+1)) != '>' ) { |
| 1108 |
if ( c == EOF || c == '<' ) |
| 1109 |
return 0; |
| 1110 |
if ( is_flag_set(f->flags, MKD_STRICT) ) { |
| 1111 |
if ( c == '`' ) |
| 1112 |
return 0; |
| 1113 |
} |
| 1114 |
size++; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
if ( size > 0 ) { |
| 1119 |
if ( process_possible_link(f, size) ) { |
| 1120 |
shift(f, size+1); |
| 1121 |
return 1; |
| 1122 |
} |
| 1123 |
else { |
| 1124 |
int i; |
| 1125 |
|
| 1126 |
if ( forbidden_tag(f) ) |
| 1127 |
return 0; |
| 1128 |
|
| 1129 |
for ( i=0; i <= size+1; i++ ) { |
| 1130 |
c = peek(f,i); |
| 1131 |
|
| 1132 |
if ( (c == '&') && (i > 0) ) |
| 1133 |
Qstring("&", f); |
| 1134 |
else |
| 1135 |
Qchar(c, f); |
| 1136 |
} |
| 1137 |
|
| 1138 |
shift(f, size+1); |
| 1139 |
return 1; |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
return 0; |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
static int |
| 1152 |
maybe_autolink(MMIOT *f) |
| 1153 |
{ |
| 1154 |
register int c; |
| 1155 |
int size; |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
for ( size=0; (c=peek(f, size+1)) != EOF; size++ ) { |
| 1160 |
if ( c == '\\' ) { |
| 1161 |
if ( peek(f, size+2) != EOF ) |
| 1162 |
++size; |
| 1163 |
} |
| 1164 |
else if ( c & 0x80 ) |
| 1165 |
continue; |
| 1166 |
else if ( isspace(c) || strchr("'\"()[]{}<>`", c) || c == MKD_EOLN ) |
| 1167 |
break; |
| 1168 |
} |
| 1169 |
|
| 1170 |
if ( (size > 1) && process_possible_link(f, size) ) { |
| 1171 |
shift(f, size); |
| 1172 |
return 1; |
| 1173 |
} |
| 1174 |
return 0; |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
static int |
| 1181 |
smartyquote(int *flags, char typeofquote, MMIOT *f) |
| 1182 |
{ |
| 1183 |
int bit = (typeofquote == 's') ? 0x01 : 0x02; |
| 1184 |
|
| 1185 |
if ( bit & (*flags) ) { |
| 1186 |
if ( isthisnonword(f,1) ) { |
| 1187 |
Qprintf(f, "&r%cquo;", typeofquote); |
| 1188 |
(*flags) &= ~bit; |
| 1189 |
return 1; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
else if ( isthisnonword(f,-1) && peek(f,1) != EOF ) { |
| 1193 |
Qprintf(f, "&l%cquo;", typeofquote); |
| 1194 |
(*flags) |= bit; |
| 1195 |
return 1; |
| 1196 |
} |
| 1197 |
return 0; |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
static int |
| 1202 |
islike(MMIOT *f, char *s) |
| 1203 |
{ |
| 1204 |
int len; |
| 1205 |
int i; |
| 1206 |
|
| 1207 |
if ( s[0] == '|' ) { |
| 1208 |
if ( !isthisnonword(f, -1) ) |
| 1209 |
return 0; |
| 1210 |
++s; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if ( !(len = strlen(s)) ) |
| 1214 |
return 0; |
| 1215 |
|
| 1216 |
if ( s[len-1] == '|' ) { |
| 1217 |
if ( !isthisnonword(f,len-1) ) |
| 1218 |
return 0; |
| 1219 |
len--; |
| 1220 |
} |
| 1221 |
|
| 1222 |
for (i=1; i < len; i++) |
| 1223 |
if (tolower(peek(f,i)) != s[i]) |
| 1224 |
return 0; |
| 1225 |
return 1; |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
static struct smarties { |
| 1230 |
char c0; |
| 1231 |
char *pat; |
| 1232 |
char *entity; |
| 1233 |
int shift; |
| 1234 |
} smarties[] = { |
| 1235 |
{ '\'', "'s|", "rsquo", 0 }, |
| 1236 |
{ '\'', "'t|", "rsquo", 0 }, |
| 1237 |
{ '\'', "'re|", "rsquo", 0 }, |
| 1238 |
{ '\'', "'ll|", "rsquo", 0 }, |
| 1239 |
{ '\'', "'ve|", "rsquo", 0 }, |
| 1240 |
{ '\'', "'m|", "rsquo", 0 }, |
| 1241 |
{ '\'', "'d|", "rsquo", 0 }, |
| 1242 |
{ '-', "---", "mdash", 2 }, |
| 1243 |
{ '-', "--", "ndash", 1 }, |
| 1244 |
{ '.', "...", "hellip", 2 }, |
| 1245 |
{ '.', ". . .", "hellip", 4 }, |
| 1246 |
{ '(', "(c)", "copy", 2 }, |
| 1247 |
{ '(', "(r)", "reg", 2 }, |
| 1248 |
{ '(', "(tm)", "trade", 3 }, |
| 1249 |
{ '3', "|3/4|", "frac34", 2 }, |
| 1250 |
{ '3', "|3/4ths|", "frac34", 2 }, |
| 1251 |
{ '1', "|1/2|", "frac12", 2 }, |
| 1252 |
{ '1', "|1/4|", "frac14", 2 }, |
| 1253 |
{ '1', "|1/4th|", "frac14", 2 }, |
| 1254 |
{ '&', "�", 0, 3 }, |
| 1255 |
} ; |
| 1256 |
#define NRSMART ( sizeof smarties / sizeof smarties[0] ) |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
static int |
| 1262 |
smartypants(int c, int *flags, MMIOT *f) |
| 1263 |
{ |
| 1264 |
int i; |
| 1265 |
|
| 1266 |
if ( is_flag_set(f->flags, MKD_NOPANTS) |
| 1267 |
|| is_flag_set(f->flags, MKD_TAGTEXT) |
| 1268 |
|| is_flag_set(f->flags, IS_LABEL) ) |
| 1269 |
return 0; |
| 1270 |
|
| 1271 |
for ( i=0; i < NRSMART; i++) |
| 1272 |
if ( (c == smarties[i].c0) && islike(f, smarties[i].pat) ) { |
| 1273 |
if ( smarties[i].entity ) |
| 1274 |
Qprintf(f, "&%s;", smarties[i].entity); |
| 1275 |
shift(f, smarties[i].shift); |
| 1276 |
return 1; |
| 1277 |
} |
| 1278 |
|
| 1279 |
switch (c) { |
| 1280 |
case '<' : return 0; |
| 1281 |
case '\'': if ( smartyquote(flags, 's', f) ) return 1; |
| 1282 |
break; |
| 1283 |
|
| 1284 |
case '"': if ( smartyquote(flags, 'd', f) ) return 1; |
| 1285 |
break; |
| 1286 |
|
| 1287 |
case '`': if ( peek(f, 1) == '`' ) { |
| 1288 |
int j = 2; |
| 1289 |
|
| 1290 |
while ( (c=peek(f,j)) != EOF ) { |
| 1291 |
if ( c == '\\' ) |
| 1292 |
j += 2; |
| 1293 |
else if ( c == '`' ) |
| 1294 |
break; |
| 1295 |
else if ( c == '\'' && peek(f, j+1) == '\'' ) { |
| 1296 |
Qstring("“", f); |
| 1297 |
___mkd_reparse(cursor(f)+1, j-2, 0, f, 0); |
| 1298 |
Qstring("”", f); |
| 1299 |
shift(f,j+1); |
| 1300 |
return 1; |
| 1301 |
} |
| 1302 |
else ++j; |
| 1303 |
} |
| 1304 |
|
| 1305 |
} |
| 1306 |
break; |
| 1307 |
} |
| 1308 |
return 0; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
static int |
| 1316 |
mathhandler(MMIOT *f, int e1, int e2) |
| 1317 |
{ |
| 1318 |
int i = 0; |
| 1319 |
|
| 1320 |
while(peek(f, ++i) != EOF) { |
| 1321 |
if (peek(f, i) == e1 && peek(f, i+1) == e2) { |
| 1322 |
cputc(peek(f,-1), f); |
| 1323 |
cputc(peek(f, 0), f); |
| 1324 |
while ( i-- > -1 ) |
| 1325 |
cputc(pull(f), f); |
| 1326 |
return 1; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
return 0; |
| 1330 |
} |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
static int |
| 1338 |
tickhandler(MMIOT *f, int tickchar, int minticks, int allow_space, spanhandler spanner) |
| 1339 |
{ |
| 1340 |
int endticks, size; |
| 1341 |
int tick = nrticks(0, tickchar, f); |
| 1342 |
|
| 1343 |
if ( !allow_space && isspace(peek(f,tick)) ) |
| 1344 |
return 0; |
| 1345 |
|
| 1346 |
if ( (tick >= minticks) && (size = matchticks(f,tickchar,tick,&endticks)) ) { |
| 1347 |
if ( endticks < tick ) { |
| 1348 |
size += (tick - endticks); |
| 1349 |
tick = endticks; |
| 1350 |
} |
| 1351 |
|
| 1352 |
shift(f, tick); |
| 1353 |
(*spanner)(f,size); |
| 1354 |
shift(f, size+tick-1); |
| 1355 |
return 1; |
| 1356 |
} |
| 1357 |
return 0; |
| 1358 |
} |
| 1359 |
|
| 1360 |
#define tag_text(f) is_flag_set(f->flags, MKD_TAGTEXT) |
| 1361 |
|
| 1362 |
|
| 1363 |
static void |
| 1364 |
text(MMIOT *f) |
| 1365 |
{ |
| 1366 |
int c, j; |
| 1367 |
int rep; |
| 1368 |
int smartyflags = 0; |
| 1369 |
|
| 1370 |
while (1) { |
| 1371 |
if ( is_flag_set(f->flags, MKD_AUTOLINK) && isalpha(peek(f,1)) && !tag_text(f) ) |
| 1372 |
maybe_autolink(f); |
| 1373 |
|
| 1374 |
c = pull(f); |
| 1375 |
|
| 1376 |
if (c == EOF) |
| 1377 |
break; |
| 1378 |
|
| 1379 |
if ( smartypants(c, &smartyflags, f) ) |
| 1380 |
continue; |
| 1381 |
switch (c) { |
| 1382 |
case 0: break; |
| 1383 |
|
| 1384 |
case MKD_EOLN: |
| 1385 |
Qstring(tag_text(f) ? " " : "<br/>", f); |
| 1386 |
break; |
| 1387 |
|
| 1388 |
case '>': if ( tag_text(f) ) |
| 1389 |
Qstring(">", f); |
| 1390 |
else |
| 1391 |
Qchar(c, f); |
| 1392 |
break; |
| 1393 |
|
| 1394 |
case '"': if ( tag_text(f) ) |
| 1395 |
Qstring(""", f); |
| 1396 |
else |
| 1397 |
Qchar(c, f); |
| 1398 |
break; |
| 1399 |
|
| 1400 |
case '!': if ( peek(f,1) == '[' ) { |
| 1401 |
pull(f); |
| 1402 |
if ( tag_text(f) || !linkylinky(1, f) ) |
| 1403 |
Qstring("![", f); |
| 1404 |
} |
| 1405 |
else |
| 1406 |
Qchar(c, f); |
| 1407 |
break; |
| 1408 |
|
| 1409 |
case '[': if ( tag_text(f) || !linkylinky(0, f) ) |
| 1410 |
Qchar(c, f); |
| 1411 |
break; |
| 1412 |
|
| 1413 |
#ifdef TYPORA |
| 1414 |
case '=': if ( is_flag_set(f->flags, MKD_NOSUPERSCRIPT) |
| 1415 |
|| is_flag_set(f->flags, MKD_STRICT) |
| 1416 |
|| is_flag_set(f->flags, MKD_TAGTEXT) |
| 1417 |
|| ! tickhandler(f,c,2,0, highlightspan)) |
| 1418 |
Qchar(c, f); |
| 1419 |
break; |
| 1420 |
|
| 1421 |
|
| 1422 |
case '^': if ( is_flag_set(f->flags, MKD_NOSUPERSCRIPT) |
| 1423 |
|| is_flag_set(f->flags, MKD_STRICT) |
| 1424 |
|| is_flag_set(f->flags, MKD_TAGTEXT) |
| 1425 |
|| ! tickhandler(f,c,1,0, supspan)) |
| 1426 |
Qchar(c, f); |
| 1427 |
break; |
| 1428 |
#else |
| 1429 |
|
| 1430 |
case '^': if ( is_flag_set(f->flags, MKD_NOSUPERSCRIPT) |
| 1431 |
|| is_flag_set(f->flags, MKD_STRICT) |
| 1432 |
|| is_flag_set(f->flags, MKD_TAGTEXT) |
| 1433 |
|| (f->last == 0) |
| 1434 |
|| ((ispunct(f->last) || isspace(f->last)) |
| 1435 |
&& f->last != ')') |
| 1436 |
|| isthisspace(f,1) ) |
| 1437 |
Qchar(c,f); |
| 1438 |
else { |
| 1439 |
char *sup = cursor(f); |
| 1440 |
int len = 0; |
| 1441 |
|
| 1442 |
if ( peek(f,1) == '(' ) { |
| 1443 |
int here = mmiottell(f); |
| 1444 |
pull(f); |
| 1445 |
|
| 1446 |
if ( (len = parenthetical('(',')',f)) <= 0 ) { |
| 1447 |
mmiotseek(f,here); |
| 1448 |
Qchar(c, f); |
| 1449 |
break; |
| 1450 |
} |
| 1451 |
sup++; |
| 1452 |
} |
| 1453 |
else { |
| 1454 |
while ( isthisalnum(f,1+len) ) |
| 1455 |
++len; |
| 1456 |
if ( !len ) { |
| 1457 |
Qchar(c,f); |
| 1458 |
break; |
| 1459 |
} |
| 1460 |
shift(f,len); |
| 1461 |
} |
| 1462 |
Qstring("<sup>",f); |
| 1463 |
___mkd_reparse(sup, len, 0, f, "()"); |
| 1464 |
Qstring("</sup>", f); |
| 1465 |
} |
| 1466 |
break; |
| 1467 |
#endif |
| 1468 |
case '_': |
| 1469 |
|
| 1470 |
if ( !(is_flag_set(f->flags, MKD_NORELAXED) || is_flag_set(f->flags, MKD_STRICT)) |
| 1471 |
&& isthisalnum(f,-1) && isthisalnum(f,1) ) { |
| 1472 |
Qchar(c, f); |
| 1473 |
break; |
| 1474 |
} |
| 1475 |
case '*': |
| 1476 |
|
| 1477 |
|
| 1478 |
if ( isthisspace(f,-1) && isthisspace(f,1) ) { |
| 1479 |
Qchar(c, f); |
| 1480 |
break; |
| 1481 |
} |
| 1482 |
|
| 1483 |
if ( tag_text(f) ) |
| 1484 |
Qchar(c, f); |
| 1485 |
else { |
| 1486 |
for (rep = 1; peek(f,1) == c; pull(f) ) |
| 1487 |
++rep; |
| 1488 |
Qem(f,c,rep); |
| 1489 |
} |
| 1490 |
break; |
| 1491 |
|
| 1492 |
#ifdef TYPORA |
| 1493 |
#define ticktick(f,c) (tickhandler(f,c,2,0,delspan) || tickhandler(f,c,1,0,subspan)) |
| 1494 |
#else |
| 1495 |
#define ticktick(f,c) tickhandler(f,c,2,0,delspan) |
| 1496 |
#endif |
| 1497 |
|
| 1498 |
case '~': if ( is_flag_set(f->flags, MKD_NOSTRIKETHROUGH) |
| 1499 |
|| is_flag_set(f->flags, MKD_STRICT) |
| 1500 |
|| is_flag_set(f->flags, MKD_TAGTEXT) |
| 1501 |
|| !ticktick(f,c) ) |
| 1502 |
Qchar(c, f); |
| 1503 |
break; |
| 1504 |
|
| 1505 |
case '`': if ( tag_text(f) || !tickhandler(f,c,1,1,codespan) ) |
| 1506 |
Qchar(c, f); |
| 1507 |
break; |
| 1508 |
|
| 1509 |
case '\\': switch ( c = pull(f) ) { |
| 1510 |
case '&': Qstring("&", f); |
| 1511 |
break; |
| 1512 |
case '<': c = peek(f,1); |
| 1513 |
if ( (c == EOF) || isspace(c) ) |
| 1514 |
Qstring("<", f); |
| 1515 |
else { |
| 1516 |
|
| 1517 |
|
| 1518 |
Qchar('\\', f); |
| 1519 |
shift(f, -1); |
| 1520 |
} |
| 1521 |
|
| 1522 |
break; |
| 1523 |
case '^': if ( is_flag_set(f->flags, MKD_STRICT) |
| 1524 |
|| is_flag_set(f->flags, MKD_NOSUPERSCRIPT) ) { |
| 1525 |
Qchar('\\', f); |
| 1526 |
shift(f,-1); |
| 1527 |
break; |
| 1528 |
} |
| 1529 |
Qchar(c, f); |
| 1530 |
break; |
| 1531 |
|
| 1532 |
case ':': case '|': |
| 1533 |
if ( is_flag_set(f->flags, MKD_NOTABLES) ) { |
| 1534 |
Qchar('\\', f); |
| 1535 |
shift(f,-1); |
| 1536 |
break; |
| 1537 |
} |
| 1538 |
Qchar(c, f); |
| 1539 |
break; |
| 1540 |
|
| 1541 |
case EOF: Qchar('\\', f); |
| 1542 |
break; |
| 1543 |
|
| 1544 |
case '[': |
| 1545 |
case '(': if ( is_flag_set(f->flags, MKD_LATEX) |
| 1546 |
&& mathhandler(f, '\\', (c =='(')?')':']') ) |
| 1547 |
break; |
| 1548 |
|
| 1549 |
|
| 1550 |
default: if ( escaped(f,c) || |
| 1551 |
strchr(">#.-+{}]![*_\\()`", c) ) |
| 1552 |
Qchar(c, f); |
| 1553 |
else { |
| 1554 |
Qchar('\\', f); |
| 1555 |
shift(f, -1); |
| 1556 |
} |
| 1557 |
break; |
| 1558 |
} |
| 1559 |
break; |
| 1560 |
|
| 1561 |
case '<': if ( !maybe_tag_or_link(f) ) { |
| 1562 |
if ( is_flag_set(f->flags, MKD_STRICT) && is_a_strict_tag_prefix(peek(f,1)) ) |
| 1563 |
Qchar(c, f); |
| 1564 |
else |
| 1565 |
Qstring("<", f); |
| 1566 |
} |
| 1567 |
break; |
| 1568 |
|
| 1569 |
case '&': j = (peek(f,1) == '#' ) ? 2 : 1; |
| 1570 |
while ( isthisalnum(f,j) ) |
| 1571 |
++j; |
| 1572 |
|
| 1573 |
if ( peek(f,j) != ';' ) |
| 1574 |
Qstring("&", f); |
| 1575 |
else |
| 1576 |
Qchar(c, f); |
| 1577 |
break; |
| 1578 |
|
| 1579 |
case '$': if ( is_flag_set(f->flags, MKD_LATEX) && (peek(f, 1) == '$') ) { |
| 1580 |
pull(f); |
| 1581 |
if ( mathhandler(f, '$', '$') ) |
| 1582 |
break; |
| 1583 |
Qchar('$', f); |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
default: f->last = c; |
| 1588 |
Qchar(c, f); |
| 1589 |
break; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
S(f->in) = f->isp = 0; |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
static void |
| 1600 |
printheader(Paragraph *pp, MMIOT *f) |
| 1601 |
{ |
| 1602 |
if ( is_flag_set(f->flags, MKD_IDANCHOR) ) { |
| 1603 |
Qprintf(f, "<h%d", pp->hnumber); |
| 1604 |
if ( is_flag_set(f->flags, MKD_TOC) ) { |
| 1605 |
Qstring(" id=\"", f); |
| 1606 |
Qanchor(pp->text, f); |
| 1607 |
Qchar('"', f); |
| 1608 |
} |
| 1609 |
Qchar('>', f); |
| 1610 |
} else { |
| 1611 |
if ( is_flag_set(f->flags, MKD_TOC) ) { |
| 1612 |
Qstring("<a name=\"", f); |
| 1613 |
Qanchor(pp->text, f); |
| 1614 |
Qstring("\"></a>\n", f); |
| 1615 |
} |
| 1616 |
Qprintf(f, "<h%d>", pp->hnumber); |
| 1617 |
} |
| 1618 |
push(T(pp->text->text), S(pp->text->text), f); |
| 1619 |
text(f); |
| 1620 |
Qprintf(f, "</h%d>", pp->hnumber); |
| 1621 |
} |
| 1622 |
|
| 1623 |
|
| 1624 |
enum e_alignments { a_NONE, a_CENTER, a_LEFT, a_RIGHT }; |
| 1625 |
|
| 1626 |
static char* alignments[] = { "", " style=\"text-align:center;\"", |
| 1627 |
" style=\"text-align:left;\"", |
| 1628 |
" style=\"text-align:right;\"" }; |
| 1629 |
|
| 1630 |
typedef STRING(int) Istring; |
| 1631 |
|
| 1632 |
static int |
| 1633 |
splat(Line *p, char *block, Istring align, int force, MMIOT *f) |
| 1634 |
{ |
| 1635 |
int first, |
| 1636 |
idx = p->dle, |
| 1637 |
colno = 0; |
| 1638 |
|
| 1639 |
|
| 1640 |
___mkd_tidy(&p->text); |
| 1641 |
if ( T(p->text)[S(p->text)-1] == '|' ) |
| 1642 |
--S(p->text); |
| 1643 |
|
| 1644 |
Qstring("<tr>\n", f); |
| 1645 |
while ( idx < S(p->text) ) { |
| 1646 |
first = idx; |
| 1647 |
if ( force && (colno >= S(align)-1) ) |
| 1648 |
idx = S(p->text); |
| 1649 |
else |
| 1650 |
while ( (idx < S(p->text)) && (T(p->text)[idx] != '|') ) { |
| 1651 |
if ( T(p->text)[idx] == '\\' ) |
| 1652 |
++idx; |
| 1653 |
++idx; |
| 1654 |
} |
| 1655 |
|
| 1656 |
Qprintf(f, "<%s%s>", |
| 1657 |
block, |
| 1658 |
alignments[ (colno < S(align)) ? T(align)[colno] : a_NONE ]); |
| 1659 |
___mkd_reparse(T(p->text)+first, idx-first, 0, f, "|"); |
| 1660 |
Qprintf(f, "</%s>\n", block); |
| 1661 |
idx++; |
| 1662 |
colno++; |
| 1663 |
} |
| 1664 |
if ( force ) |
| 1665 |
while (colno < S(align) ) { |
| 1666 |
Qprintf(f, "<%s></%s>\n", block, block); |
| 1667 |
++colno; |
| 1668 |
} |
| 1669 |
Qstring("</tr>\n", f); |
| 1670 |
return colno; |
| 1671 |
} |
| 1672 |
|
| 1673 |
|
| 1674 |
static int |
| 1675 |
printtable(Paragraph *pp, MMIOT *f) |
| 1676 |
{ |
| 1677 |
|
| 1678 |
|
| 1679 |
Line *hdr, *dash, *body; |
| 1680 |
Istring align; |
| 1681 |
int hcols,start; |
| 1682 |
char *p; |
| 1683 |
enum e_alignments it; |
| 1684 |
|
| 1685 |
hdr = pp->text; |
| 1686 |
dash= hdr->next; |
| 1687 |
body= dash->next; |
| 1688 |
|
| 1689 |
if ( T(hdr->text)[hdr->dle] == '|' ) { |
| 1690 |
|
| 1691 |
|
| 1692 |
Line *r; |
| 1693 |
for ( r = pp->text; r; r = r->next ) |
| 1694 |
r->dle ++; |
| 1695 |
} |
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
CREATE(align); |
| 1700 |
|
| 1701 |
for (p=T(dash->text), start=dash->dle; start < S(dash->text); ) { |
| 1702 |
char first, last; |
| 1703 |
int end; |
| 1704 |
|
| 1705 |
last=first=0; |
| 1706 |
for (end=start ; (end < S(dash->text)) && p[end] != '|'; ++ end ) { |
| 1707 |
if ( p[end] == '\\' ) |
| 1708 |
++ end; |
| 1709 |
else if ( !isspace(p[end]) ) { |
| 1710 |
if ( !first) first = p[end]; |
| 1711 |
last = p[end]; |
| 1712 |
} |
| 1713 |
} |
| 1714 |
it = ( first == ':' ) ? (( last == ':') ? a_CENTER : a_LEFT) |
| 1715 |
: (( last == ':') ? a_RIGHT : a_NONE ); |
| 1716 |
|
| 1717 |
EXPAND(align) = it; |
| 1718 |
start = 1+end; |
| 1719 |
} |
| 1720 |
|
| 1721 |
Qstring("<table>\n", f); |
| 1722 |
Qstring("<thead>\n", f); |
| 1723 |
hcols = splat(hdr, "th", align, 0, f); |
| 1724 |
Qstring("</thead>\n", f); |
| 1725 |
|
| 1726 |
if ( hcols < S(align) ) |
| 1727 |
S(align) = hcols; |
| 1728 |
else |
| 1729 |
while ( hcols > S(align) ) |
| 1730 |
EXPAND(align) = a_NONE; |
| 1731 |
|
| 1732 |
Qstring("<tbody>\n", f); |
| 1733 |
for ( ; body; body = body->next) |
| 1734 |
splat(body, "td", align, 1, f); |
| 1735 |
Qstring("</tbody>\n", f); |
| 1736 |
Qstring("</table>\n", f); |
| 1737 |
|
| 1738 |
DELETE(align); |
| 1739 |
return 1; |
| 1740 |
} |
| 1741 |
|
| 1742 |
|
| 1743 |
static int |
| 1744 |
printblock(Paragraph *pp, MMIOT *f) |
| 1745 |
{ |
| 1746 |
static char *Begin[] = { "", "<p>", "<p style=\"text-align:center;\">" }; |
| 1747 |
static char *End[] = { "", "</p>","</p>" }; |
| 1748 |
Line *t = pp->text; |
| 1749 |
int align = pp->align; |
| 1750 |
|
| 1751 |
while (t) { |
| 1752 |
if ( S(t->text) ) { |
| 1753 |
if ( t->next && S(t->text) > 2 |
| 1754 |
&& T(t->text)[S(t->text)-2] == ' ' |
| 1755 |
&& T(t->text)[S(t->text)-1] == ' ' ) { |
| 1756 |
push(T(t->text), S(t->text)-2, f); |
| 1757 |
pushc(MKD_EOLN, f); |
| 1758 |
pushc('\n', f); |
| 1759 |
} |
| 1760 |
else { |
| 1761 |
___mkd_tidy(&t->text); |
| 1762 |
push(T(t->text), S(t->text), f); |
| 1763 |
if ( t->next ) |
| 1764 |
pushc('\n', f); |
| 1765 |
} |
| 1766 |
} |
| 1767 |
t = t->next; |
| 1768 |
} |
| 1769 |
Qstring(Begin[align], f); |
| 1770 |
text(f); |
| 1771 |
Qstring(End[align], f); |
| 1772 |
return 1; |
| 1773 |
} |
| 1774 |
|
| 1775 |
|
| 1776 |
static void |
| 1777 |
printcode(Line *t, char *lang, MMIOT *f) |
| 1778 |
{ |
| 1779 |
int blanks; |
| 1780 |
|
| 1781 |
if ( f->cb->e_codefmt ) { |
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
char *text; |
| 1787 |
char *fmt; |
| 1788 |
int size, copy_p; |
| 1789 |
Line *p; |
| 1790 |
|
| 1791 |
for (size=0, p = t; p; p = p->next ) |
| 1792 |
size += 1+S(p->text); |
| 1793 |
|
| 1794 |
text = malloc(1+size); |
| 1795 |
|
| 1796 |
for ( copy_p = 0; t ; t = t->next ) { |
| 1797 |
memcpy(text+copy_p, T(t->text), S(t->text)); |
| 1798 |
copy_p += S(t->text); |
| 1799 |
text[copy_p++] = '\n'; |
| 1800 |
} |
| 1801 |
text[copy_p] = 0; |
| 1802 |
|
| 1803 |
fmt = (*(f->cb->e_codefmt))(text, copy_p, (lang && lang[0]) ? lang : 0); |
| 1804 |
free(text); |
| 1805 |
|
| 1806 |
if ( fmt ) { |
| 1807 |
Qwrite(fmt, strlen(fmt), f); |
| 1808 |
if ( f->cb->e_free ) |
| 1809 |
(*(f->cb->e_free))(fmt, f->cb->e_data); |
| 1810 |
return; |
| 1811 |
} |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
} |
| 1816 |
|
| 1817 |
Qstring("<pre><code", f); |
| 1818 |
if (lang && lang[0]) { |
| 1819 |
Qstring(" class=\"", f); |
| 1820 |
Qstring(lang, f); |
| 1821 |
Qstring("\"", f); |
| 1822 |
} |
| 1823 |
Qstring(">", f); |
| 1824 |
for ( blanks = 0; t ; t = t->next ) { |
| 1825 |
if ( S(t->text) > t->dle ) { |
| 1826 |
while ( blanks ) { |
| 1827 |
Qchar('\n', f); |
| 1828 |
--blanks; |
| 1829 |
} |
| 1830 |
code(f, T(t->text), S(t->text)); |
| 1831 |
Qchar('\n', f); |
| 1832 |
} |
| 1833 |
else blanks++; |
| 1834 |
} |
| 1835 |
Qstring("</code></pre>", f); |
| 1836 |
} |
| 1837 |
|
| 1838 |
|
| 1839 |
static void |
| 1840 |
printhtml(Line *t, MMIOT *f) |
| 1841 |
{ |
| 1842 |
int blanks; |
| 1843 |
|
| 1844 |
for ( blanks=0; t ; t = t->next ) |
| 1845 |
if ( S(t->text) ) { |
| 1846 |
for ( ; blanks; --blanks ) |
| 1847 |
Qchar('\n', f); |
| 1848 |
|
| 1849 |
Qwrite(T(t->text), S(t->text), f); |
| 1850 |
Qchar('\n', f); |
| 1851 |
} |
| 1852 |
else |
| 1853 |
blanks++; |
| 1854 |
} |
| 1855 |
|
| 1856 |
|
| 1857 |
static void |
| 1858 |
htmlify_paragraphs(Paragraph *p, MMIOT *f) |
| 1859 |
{ |
| 1860 |
___mkd_emblock(f); |
| 1861 |
|
| 1862 |
while (( p = display(p, f) )) { |
| 1863 |
___mkd_emblock(f); |
| 1864 |
Qstring("\n\n", f); |
| 1865 |
} |
| 1866 |
} |
| 1867 |
|
| 1868 |
|
| 1869 |
#ifdef GITHUB_CHECKBOX |
| 1870 |
static void |
| 1871 |
li_htmlify(Paragraph *p, char *arguments, mkd_flag_t flags, MMIOT *f) |
| 1872 |
{ |
| 1873 |
___mkd_emblock(f); |
| 1874 |
|
| 1875 |
Qprintf(f, "<li"); |
| 1876 |
if ( arguments ) |
| 1877 |
Qprintf(f, " %s", arguments); |
| 1878 |
if ( flags & GITHUB_CHECK ) |
| 1879 |
Qprintf(f, " class=\"github_checkbox\""); |
| 1880 |
Qprintf(f, ">"); |
| 1881 |
#if CHECKBOX_AS_INPUT |
| 1882 |
if ( flags & GITHUB_CHECK ) { |
| 1883 |
Qprintf(f, "<input disabled=\"\" type=\"checkbox\""); |
| 1884 |
if ( flags & IS_CHECKED ) |
| 1885 |
Qprintf(f, " checked=\"checked\""); |
| 1886 |
Qprintf(f, "/>"); |
| 1887 |
} |
| 1888 |
#else |
| 1889 |
if ( flags & GITHUB_CHECK ) |
| 1890 |
Qprintf(f, flags & IS_CHECKED ? "☑" : "☐"); |
| 1891 |
#endif |
| 1892 |
|
| 1893 |
htmlify_paragraphs(p, f); |
| 1894 |
|
| 1895 |
Qprintf(f, "</li>"); |
| 1896 |
___mkd_emblock(f); |
| 1897 |
} |
| 1898 |
#endif |
| 1899 |
|
| 1900 |
|
| 1901 |
static void |
| 1902 |
htmlify(Paragraph *p, char *block, char *arguments, MMIOT *f) |
| 1903 |
{ |
| 1904 |
___mkd_emblock(f); |
| 1905 |
if ( block ) |
| 1906 |
Qprintf(f, arguments ? "<%s %s>" : "<%s>", block, arguments); |
| 1907 |
|
| 1908 |
htmlify_paragraphs(p, f); |
| 1909 |
|
| 1910 |
if ( block ) |
| 1911 |
Qprintf(f, "</%s>", block); |
| 1912 |
___mkd_emblock(f); |
| 1913 |
} |
| 1914 |
|
| 1915 |
|
| 1916 |
static void |
| 1917 |
definitionlist(Paragraph *p, MMIOT *f) |
| 1918 |
{ |
| 1919 |
Line *tag; |
| 1920 |
|
| 1921 |
if ( p ) { |
| 1922 |
Qstring("<dl>\n", f); |
| 1923 |
|
| 1924 |
for ( ; p ; p = p->next) { |
| 1925 |
for ( tag = p->text; tag; tag = tag->next ) { |
| 1926 |
Qstring("<dt>", f); |
| 1927 |
___mkd_reparse(T(tag->text), S(tag->text), 0, f, 0); |
| 1928 |
Qstring("</dt>\n", f); |
| 1929 |
} |
| 1930 |
|
| 1931 |
htmlify(p->down, "dd", p->ident, f); |
| 1932 |
Qchar('\n', f); |
| 1933 |
} |
| 1934 |
|
| 1935 |
Qstring("</dl>", f); |
| 1936 |
} |
| 1937 |
} |
| 1938 |
|
| 1939 |
|
| 1940 |
static void |
| 1941 |
listdisplay(int typ, Paragraph *p, MMIOT* f) |
| 1942 |
{ |
| 1943 |
if ( p ) { |
| 1944 |
Qprintf(f, "<%cl", (typ==UL)?'u':'o'); |
| 1945 |
if ( typ == AL ) |
| 1946 |
Qprintf(f, " type=\"a\""); |
| 1947 |
Qprintf(f, ">\n"); |
| 1948 |
|
| 1949 |
for ( ; p ; p = p->next ) { |
| 1950 |
#ifdef GITHUB_CHECKBOX |
| 1951 |
li_htmlify(p->down, p->ident, p->flags, f); |
| 1952 |
#else |
| 1953 |
htmlify(p->down, "li", p->ident, f); |
| 1954 |
#endif |
| 1955 |
Qchar('\n', f); |
| 1956 |
} |
| 1957 |
|
| 1958 |
Qprintf(f, "</%cl>\n", (typ==UL)?'u':'o'); |
| 1959 |
} |
| 1960 |
} |
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
static Paragraph* |
| 1966 |
display(Paragraph *p, MMIOT *f) |
| 1967 |
{ |
| 1968 |
if ( !p ) return 0; |
| 1969 |
|
| 1970 |
switch ( p->typ ) { |
| 1971 |
case STYLE: |
| 1972 |
case WHITESPACE: |
| 1973 |
break; |
| 1974 |
|
| 1975 |
case HTML: |
| 1976 |
printhtml(p->text, f); |
| 1977 |
break; |
| 1978 |
|
| 1979 |
case CODE: |
| 1980 |
printcode(p->text, p->lang, f); |
| 1981 |
break; |
| 1982 |
|
| 1983 |
case QUOTE: |
| 1984 |
htmlify(p->down, p->ident ? "div" : "blockquote", p->ident, f); |
| 1985 |
break; |
| 1986 |
|
| 1987 |
case UL: |
| 1988 |
case OL: |
| 1989 |
case AL: |
| 1990 |
listdisplay(p->typ, p->down, f); |
| 1991 |
break; |
| 1992 |
|
| 1993 |
case DL: |
| 1994 |
definitionlist(p->down, f); |
| 1995 |
break; |
| 1996 |
|
| 1997 |
case HR: |
| 1998 |
Qstring("<hr />", f); |
| 1999 |
break; |
| 2000 |
|
| 2001 |
case HDR: |
| 2002 |
printheader(p, f); |
| 2003 |
break; |
| 2004 |
|
| 2005 |
case TABLE: |
| 2006 |
printtable(p, f); |
| 2007 |
break; |
| 2008 |
|
| 2009 |
case SOURCE: |
| 2010 |
htmlify(p->down, 0, 0, f); |
| 2011 |
break; |
| 2012 |
|
| 2013 |
default: |
| 2014 |
printblock(p, f); |
| 2015 |
break; |
| 2016 |
} |
| 2017 |
return p->next; |
| 2018 |
} |
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
static void |
| 2024 |
mkd_extra_footnotes(MMIOT *m) |
| 2025 |
{ |
| 2026 |
int j, i; |
| 2027 |
Footnote *t; |
| 2028 |
|
| 2029 |
if ( m->footnotes->reference == 0 ) |
| 2030 |
return; |
| 2031 |
|
| 2032 |
Csprintf(&m->out, "\n<div class=\"footnotes\">\n<hr/>\n<ol>\n"); |
| 2033 |
|
| 2034 |
for ( i=1; i <= m->footnotes->reference; i++ ) { |
| 2035 |
for ( j=0; j < S(m->footnotes->note); j++ ) { |
| 2036 |
t = &T(m->footnotes->note)[j]; |
| 2037 |
if ( (t->refnumber == i) && (t->flags & REFERENCED) ) { |
| 2038 |
Csprintf(&m->out, "<li id=\"%s:%d\">\n", |
| 2039 |
p_or_nothing(m), t->refnumber); |
| 2040 |
htmlify(t->text, 0, 0, m); |
| 2041 |
Csprintf(&m->out, "<a href=\"#%sref:%d\" rev=\"footnote\">↩</a>", |
| 2042 |
p_or_nothing(m), t->refnumber); |
| 2043 |
Csprintf(&m->out, "</li>\n"); |
| 2044 |
} |
| 2045 |
} |
| 2046 |
} |
| 2047 |
Csprintf(&m->out, "</ol>\n</div>\n"); |
| 2048 |
} |
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 2053 |
|
| 2054 |
int |
| 2055 |
mkd_document(Document *p, char **res) |
| 2056 |
{ |
| 2057 |
int size; |
| 2058 |
|
| 2059 |
if ( p && p->compiled ) { |
| 2060 |
if ( ! p->html ) { |
| 2061 |
htmlify(p->code, 0, 0, p->ctx); |
| 2062 |
if ( is_flag_set(p->ctx->flags, MKD_EXTRA_FOOTNOTE) ) |
| 2063 |
mkd_extra_footnotes(p->ctx); |
| 2064 |
p->html = 1; |
| 2065 |
size = S(p->ctx->out); |
| 2066 |
|
| 2067 |
if ( (size == 0) || T(p->ctx->out)[size-1] ) { |
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
EXPAND(p->ctx->out) = 0; |
| 2072 |
--S(p->ctx->out); |
| 2073 |
} |
| 2074 |
} |
| 2075 |
|
| 2076 |
*res = T(p->ctx->out); |
| 2077 |
return S(p->ctx->out); |
| 2078 |
} |
| 2079 |
return EOF; |
| 2080 |
} |