mmd.c 21.9 KB
Newer Older
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
/*
 * Implementation of miniature markdown library.
 *
 *     https://github.com/michaelrsweet/mmd
 *
 * Copyright 2017 by Michael R Sweet.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Include necessary headers...
 */

#include "mmd.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>


/*
 * Structures...
 */

struct _mmd_s
{
  mmd_type_t    type;                   /* Node type */
  int           whitespace;             /* Leading whitespace? */
  char          *text,                  /* Text */
                *url;                   /* Reference URL (image/link/etc.) */
  mmd_t         *parent,                /* Parent node */
                *first_child,           /* First child node */
                *last_child,            /* Last child node */
                *prev_sibling,          /* Previous sibling node */
                *next_sibling;          /* Next sibling node */
};


/*
 * Local functions...
 */


static mmd_t    *mmd_add(mmd_t *parent, mmd_type_t type, int whitespace, char *text, char *url);
static void     mmd_free(mmd_t *node);
static void     mmd_parse_inline(mmd_t *parent, char *line);
static char     *mmd_parse_link(char *lineptr, char **text, char **url);
static void     mmd_remove(mmd_t *node);


/*
 * 'mmdFree()' - Free a markdown tree.
 */

void
mmdFree(mmd_t *node)                    /* I - First node */
{
  mmd_t *current,		        /* Current node */
	*next;			        /* Next node */


  mmd_remove(node);

  for (current = node->first_child; current; current = next)
  {
   /*
    * Get the next node...
    */

    if ((next = current->first_child) != NULL)
    {
     /*
      * Free parent nodes after child nodes have been freed...
      */

      current->first_child = NULL;
      continue;
    }

    if ((next = current->next_sibling) == NULL)
    {
     /*
      * Next node is the parent, which we'll free as needed...
      */

      if ((next = current->parent) == node)
        next = NULL;
    }

   /*
    * Free child...
    */

    mmd_free(current);
  }

 /*
  * Then free the memory used by the parent node...
  */

  mmd_free(node);
}


/*
 * 'mmdGetFirstChild()' - Return the first child of a node, if any.
 */

mmd_t *                                 /* O - First child or @code NULL@ if none */
mmdGetFirstChild(mmd_t *node)           /* I - Node */
{
  return (node ? node->first_child : NULL);
}


/*
 * 'mmdGetLastChild()' - Return the last child of a node, if any.
 */

mmd_t *                                 /* O - Last child or @code NULL@ if none */
mmdGetLastChild(mmd_t *node)            /* I - Node */
{
  return (node ? node->last_child : NULL);
}


/*
 * 'mmdGetMetadata()' - Return the metadata for the given keyword.
 */

const char *                            /* O - Value or @code NULL@ if none */
mmdGetMetadata(mmd_t      *doc,         /* I - Document */
               const char *keyword)     /* I - Keyword */
{
  mmd_t         *metadata,              /* Metadata node */
                *current;               /* Current node */
  char          prefix[256];            /* Prefix string */
  size_t        prefix_len;             /* Length of prefix string */
  const char    *value;                 /* Pointer to value */


  if (!doc || (metadata = doc->first_child) == NULL || metadata->type != MMD_TYPE_METADATA)
    return (NULL);

  snprintf(prefix, sizeof(prefix), "%s:", keyword);
  prefix_len = strlen(prefix);

  for (current = metadata->first_child; current; current = current->next_sibling)
  {
    if (strncmp(current->text, prefix, prefix_len))
      continue;

    value = current->text + prefix_len;
    while (isspace(*value & 255))
      value ++;

    return (value);
  }

  return (NULL);
}


/*
 * 'mmdGetNextSibling()' - Return the next sibling of a node, if any.
 */

mmd_t *                                 /* O - Next sibling or @code NULL@ if none */
mmdGetNextSibling(mmd_t *node)          /* I - Node */
{
  return (node ? node->next_sibling : NULL);
}


/*
 * 'mmdGetParent()' - Return the parent of a node, if any.
 */

mmd_t *                                 /* O - Parent node or @code NULL@ if none */
mmdGetParent(mmd_t *node)               /* I - Node */
{
  return (node ? node->parent : NULL);
}


/*
 * 'mmdGetPrevSibling()' - Return the previous sibling of a node, if any.
 */

mmd_t *                                 /* O - Previous sibling or @code NULL@ if none */
mmdGetPrevSibling(mmd_t *node)          /* I - Node */
{
  return (node ? node->prev_sibling : NULL);
}


/*
 * 'mmdGetText()' - Return the text associated with a node, if any.
 */

const char *                            /* O - Text or @code NULL@ if none */
mmdGetText(mmd_t *node)                 /* I - Node */
{
  return (node ? node->text : NULL);
}


/*
 * 'mmdGetType()' - Return the type of a node, if any.
 */

mmd_type_t                              /* O - Type or @code MMD_TYPE_NONE@ if none */
mmdGetType(mmd_t *node)                 /* I - Node */
{
  return (node ? node->type : MMD_TYPE_NONE);
}


/*
 * 'mmdGetURL()' - Return the URL associated with a node, if any.
 */

const char *                            /* O - URL or @code NULL@ if none */
mmdGetURL(mmd_t *node)                  /* I - Node */
{
  return (node ? node->url : NULL);
}


/*
 * 'mmdGetWhitespace()' - Return whether whitespace preceded a node.
 */

int                                     /* O - 1 for whitespace, 0 for none */
mmdGetWhitespace(mmd_t *node)           /* I - Node */
{
  return (node ? node->whitespace : 0);
}


/*
 * 'mmdIsBlock()' - Return whether the node is a block.
 */

int                                     /* O - 1 for block nodes, 0 otherwise */
mmdIsBlock(mmd_t *node)                 /* I - Node */
{
  return (node ? node->type < MMD_TYPE_NORMAL_TEXT : 0);
}


/*
 * 'mmdLoad()' - Load a markdown file into nodes.
 */

mmd_t *                                 /* O - First node in markdown */
mmdLoad(const char *filename)           /* I - File to load */
{
  FILE          *fp;                    /* File */
Michael Sweet's avatar
Michael Sweet committed
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
  mmd_t         *doc;                   /* Document */


 /*
  * Open the file and create an empty document...
  */

  if ((fp = fopen(filename, "r")) == NULL)
    return (NULL);

  doc = mmdLoadFile(fp);

  fclose(fp);

  return (doc);
}


/*
 * 'mmdLoadFile()' - Load a markdown file into nodes from a stdio file.
 */

mmd_t *                                 /* O - First node in markdown */
mmdLoadFile(FILE *fp)                   /* I - File to load */
{
304 305 306 307 308 309 310 311 312 313 314
  mmd_t         *doc,                   /* Document */
                *current,               /* Current parent block */
                *block = NULL;          /* Current block */
  mmd_type_t    type;                   /* Type for line */
  char          line[65536],            /* Line from file */
                *lineptr,               /* Pointer into line */
                *lineend;               /* End of line */
  int           blank_code = 0;         /* Saved indented blank code line */


 /*
Michael Sweet's avatar
Michael Sweet committed
315
  * Create an empty document...
316 317 318 319
  */

  doc = current = mmd_add(NULL, MMD_TYPE_DOCUMENT, 0, NULL, NULL);

Michael Sweet's avatar
Michael Sweet committed
320 321 322 323 324 325
  if (!doc)
  {
    fclose(fp);
    return (NULL);
  }

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
 /*
  * Read lines until end-of-file...
  */

  while (fgets(line, sizeof(line), fp))
  {
    lineptr = line;

    while (isspace(*lineptr & 255))
      lineptr ++;

    if ((lineptr - line) >= 4 && !block && (current == doc || current->type == MMD_TYPE_CODE_BLOCK))
    {
     /*
      * Indented code block.
      */

      if (current == doc)
        current = mmd_add(doc, MMD_TYPE_CODE_BLOCK, 0, NULL, NULL);

      if (blank_code)
        mmd_add(current, MMD_TYPE_CODE_TEXT, 0, "\n", NULL);

      mmd_add(current, MMD_TYPE_CODE_TEXT, 0, line + 4, NULL);

      blank_code = 0;
      continue;
    }
    else if (*lineptr == '`' && (!lineptr[1] || lineptr[1] == '`'))
    {
      if (block)
      {
        if (block->type == MMD_TYPE_CODE_BLOCK)
          block = NULL;
        else if (block->type == MMD_TYPE_LIST_ITEM)
          block = mmd_add(block, MMD_TYPE_CODE_BLOCK, 0, NULL, NULL);
        else if (block->parent->type == MMD_TYPE_LIST_ITEM)
          block = mmd_add(block->parent, MMD_TYPE_CODE_BLOCK, 0, NULL, NULL);
        else
          block = mmd_add(current, MMD_TYPE_CODE_BLOCK, 0, NULL, NULL);
      }
      else
        block = mmd_add(current, MMD_TYPE_CODE_BLOCK, 0, NULL, NULL);

      continue;
    }

    if (block && block->type == MMD_TYPE_CODE_BLOCK)
    {
      mmd_add(block, MMD_TYPE_CODE_TEXT, 0, line, NULL);
      continue;
    }
    else if (!strncmp(lineptr, "---", 3) && doc->first_child == NULL)
    {
     /*
      * Document metadata...
      */

      block = mmd_add(doc, MMD_TYPE_METADATA, 0, NULL, NULL);

      while (fgets(line, sizeof(line), fp))
      {
        lineptr = line;

        while (isspace(*lineptr & 255))
          lineptr ++;

        if (!strncmp(line, "---", 3) || !strncmp(line, "...", 3))
          break;

        lineend = lineptr + strlen(lineptr) - 1;
        if (lineend > lineptr && *lineend == '\n')
          *lineend = '\0';

        mmd_add(block, MMD_TYPE_METADATA_TEXT, 0, lineptr, NULL);
      }

      block = NULL;
      continue;
    }
    else if (!block && (!strncmp(lineptr, "---", 3) || !strncmp(lineptr, "***", 3) || !strncmp(lineptr, "___", 3)))
    {
      int ch = *lineptr;

      lineptr += 3;
      while (*lineptr && (*lineptr == ch || isspace(*lineptr & 255)))
        lineptr ++;

      if (!*lineptr)
      {
        block = NULL;
        mmd_add(current, MMD_TYPE_THEMATIC_BREAK, 0, NULL, NULL);
        continue;
      }
    }

    if (*lineptr == '>')
    {
     /*
      * Block quote.  See if the parent of the current node is already a block
      * quote...
      */

      if (current == doc || current->type != MMD_TYPE_BLOCK_QUOTE)
        current = mmd_add(doc, MMD_TYPE_BLOCK_QUOTE, 0, NULL, NULL);

     /*
      * Skip whitespace after the ">"...
      */

      lineptr ++;
      while (isspace(*lineptr & 255))
        lineptr ++;
    }
    else if (current->type == MMD_TYPE_BLOCK_QUOTE)
      current = current->parent;

    if (!*lineptr)
    {
      blank_code = current->type == MMD_TYPE_CODE_BLOCK;
      block      = NULL;
      continue;
    }
    else if (!strcmp(lineptr, "+"))
    {
      if (block)
      {
        if (block->type == MMD_TYPE_LIST_ITEM)
          block = mmd_add(block, MMD_TYPE_PARAGRAPH, 0, NULL, NULL);
        else if (block->parent->type == MMD_TYPE_LIST_ITEM)
          block = mmd_add(block->parent, MMD_TYPE_PARAGRAPH, 0, NULL, NULL);
        else
          block = NULL;
      }
      continue;
    }
    else if (block && block->type == MMD_TYPE_PARAGRAPH && (!strncmp(lineptr, "---", 3) || !strncmp(lineptr, "===", 3)))
    {
      int ch = *lineptr;

      lineptr += 3;
      while (*lineptr == ch)
        lineptr ++;
      while (isspace(*lineptr & 255))
        lineptr ++;

      if (!*lineptr)
      {
        if (ch == '=')
          block->type = MMD_TYPE_HEADING_1;
        else
          block->type = MMD_TYPE_HEADING_2;

        block = NULL;
        continue;
      }
    }
    else if ((*lineptr == '-' || *lineptr == '+' || *lineptr == '*') && isspace(lineptr[1] & 255))
    {
     /*
      * Bulleted list...
      */

      lineptr += 2;
      while (isspace(*lineptr & 255))
        lineptr ++;

      if (current == doc && doc->last_child && doc->last_child->type == MMD_TYPE_UNORDERED_LIST)
        current = doc->last_child;
      else if (current->type != MMD_TYPE_UNORDERED_LIST)
        current = mmd_add(current, MMD_TYPE_UNORDERED_LIST, 0, NULL, NULL);

      type  = MMD_TYPE_LIST_ITEM;
      block = NULL;
    }
    else if (isdigit(*lineptr & 255))
    {
     /*
      * Ordered list?
      */

      char *temp = lineptr + 1;

      while (isdigit(*temp & 255))
        temp ++;

      if (*temp == '.' && isspace(temp[1] & 255))
      {
       /*
        * Yes, ordered list.
        */

        lineptr = temp + 2;
        while (isspace(*lineptr & 255))
          lineptr ++;

        if (current == doc && doc->last_child && doc->last_child->type == MMD_TYPE_ORDERED_LIST)
          current = doc->last_child;
        else if (current->type != MMD_TYPE_ORDERED_LIST)
          current = mmd_add(current, MMD_TYPE_ORDERED_LIST, 0, NULL, NULL);

        type  = MMD_TYPE_LIST_ITEM;
        block = NULL;
      }
      else
      {
       /*
        * No, just a regular paragraph...
        */

        type = block ? block->type : MMD_TYPE_PARAGRAPH;
      }
    }
    else if (*lineptr == '#')
    {
     /*
      * Heading, count the number of '#' for the heading level...
      */

      char *temp = lineptr + 1;

      while (*temp == '#')
        temp ++;

      if ((temp - lineptr) <= 6)
      {
       /*
        * Heading 1-6...
        */

        type  = MMD_TYPE_HEADING_1 + (temp - lineptr - 1);
        block = NULL;

       /*
        * Skip whitespace after "#"...
        */

        lineptr = temp;
        while (isspace(*lineptr & 255))
          lineptr ++;

       /*
        * Strip trailing "#" characters...
        */

        for (temp = lineptr + strlen(lineptr) - 1; temp > lineptr && *temp == '#'; temp --)
          *temp = '\0';
      }
      else
      {
       /*
        * More than 6 #'s, just treat as a paragraph...
        */

        type = MMD_TYPE_PARAGRAPH;
      }
Michael Sweet's avatar
Michael Sweet committed
582 583 584

      if (current->type != MMD_TYPE_BLOCK_QUOTE)
        current = doc;
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
    }
    else if (!block)
    {
      type = MMD_TYPE_PARAGRAPH;

      if (lineptr == line)
        current = doc;
    }
    else
      type = block->type;

    if (!block || block->type != type)
    {
      if (current->type == MMD_TYPE_CODE_BLOCK)
        current = doc;

      block = mmd_add(current, type, 0, NULL, NULL);
    }

    mmd_parse_inline(block, lineptr);
  }

  return (doc);
}


/*
 * 'mmd_add()' - Add a new markdown node.
 */

static mmd_t *                          /* O - New node */
mmd_add(mmd_t      *parent,             /* I - Parent node */
        mmd_type_t type,                /* I - Node type */
        int        whitespace,          /* I - 1 if whitespace precedes this node */
        char       *text,               /* I - Text, if any */
        char       *url)                /* I - URL, if any */
{
  mmd_t         *temp;                  /* New node */


  if ((temp = calloc(1, sizeof(mmd_t))) != NULL)
  {
    if (parent)
    {
     /*
      * Add node to the parent...
      */

      temp->parent = parent;

      if (parent->last_child)
      {
        parent->last_child->next_sibling = temp;
        temp->prev_sibling               = parent->last_child;
        parent->last_child               = temp;
      }
      else
      {
        parent->first_child = parent->last_child = temp;
      }
    }

   /*
    * Copy the node values...
    */

    temp->type       = type;
    temp->whitespace = whitespace;

    if (text)
      temp->text = strdup(text);

    if (url)
      temp->url = strdup(url);
  }

  return (temp);
}


/*
 * 'mmd_free()' - Free memory used by a node.
 */

static void
mmd_free(mmd_t *node)                   /* I - Node */
{
  if (node->text)
    free(node->text);

  if (node->url)
    free(node->url);

  free(node);
}


/*
 * 'mmd_parse_inline()' - Parse inline formatting.
 */

static void
mmd_parse_inline(mmd_t *parent,         /* I - Parent node */
                 char  *line)           /* I - Line from file */
{
  mmd_type_t    type;                   /* Current node type */
  int           whitespace;             /* Whitespace precedes? */
  char          *lineptr,               /* Pointer into line */
                *text,                  /* Text fragment in line */
                *url;                   /* URL in link */


  whitespace = parent->last_child != NULL;

  for (lineptr = line, text = NULL, type = MMD_TYPE_NORMAL_TEXT; *lineptr; lineptr ++)
  {
701
    if (isspace(*lineptr & 255) && type != MMD_TYPE_CODE_TEXT)
702 703 704 705 706 707 708 709 710 711
    {
      if (text)
      {
        *lineptr = '\0';
        mmd_add(parent, type, whitespace, text, NULL);
        text = NULL;
      }

      whitespace = 1;
    }
712
    else if (*lineptr == '!' && lineptr[1] == '[' && type != MMD_TYPE_CODE_TEXT)
713
    {
714 715 716 717 718
     /*
      * Image...
      */

      if (text)
719
      {
720
        mmd_add(parent, type, whitespace, text, NULL);
721

722 723
        text       = NULL;
        whitespace = 0;
724 725
      }

726 727 728 729
      lineptr = mmd_parse_link(lineptr + 1, &text, &url);

      if (url)
        mmd_add(parent, MMD_TYPE_IMAGE, whitespace, text, url);
730

731 732
      if (!*lineptr)
        return;
733

734 735 736 737
      text = url = NULL;
      whitespace = 0;
      lineptr --;
    }
738
    else if (*lineptr == '[' && type != MMD_TYPE_CODE_TEXT)
739 740 741 742
    {
     /*
      * Link...
      */
743

744 745 746 747 748
      if (text)
      {
        mmd_add(parent, type, whitespace, text, NULL);

        text       = NULL;
749 750
        whitespace = 0;
      }
751 752 753 754

      lineptr = mmd_parse_link(lineptr, &text, &url);

      if (text && *text == '`')
755
      {
756
        char *end = text + strlen(text) - 1;
757

758 759 760
        text ++;
        if (end > text && *end == '`')
          *end = '\0';
761

762 763 764 765
        mmd_add(parent, MMD_TYPE_CODE_TEXT, whitespace, text, url);
      }
      else if (text)
        mmd_add(parent, MMD_TYPE_LINKED_TEXT, whitespace, text, url);
Michael Sweet's avatar
Michael Sweet committed
766

767 768
      if (!*lineptr)
        return;
Michael Sweet's avatar
Michael Sweet committed
769

770 771 772 773
      text = url = NULL;
      whitespace = 0;
      lineptr --;
    }
774
    else if (*lineptr == '<' && type != MMD_TYPE_CODE_TEXT && strchr(lineptr + 1, '>'))
775 776 777 778
    {
     /*
      * Autolink...
      */
779

780 781 782
      if (text)
      {
        mmd_add(parent, type, whitespace, text, NULL);
783

784
        text       = NULL;
785 786 787
        whitespace = 0;
      }

788 789
      url     = lineptr + 1;
      lineptr = strchr(lineptr + 1, '>');
790

791
      *lineptr++ = '\0';
792

793
      mmd_add(parent, MMD_TYPE_LINKED_TEXT, whitespace, url, url);
794

795 796 797 798 799 800 801
      text = url = NULL;
      whitespace = 0;
      lineptr --;
    }
    else if (*lineptr == '*' && type != MMD_TYPE_CODE_TEXT)
    {
      if (text)
802
      {
803 804 805 806 807 808 809
        *lineptr = '\0';

        mmd_add(parent, type, whitespace, text, NULL);

        *lineptr   = '*';
        text       = NULL;
        whitespace = 0;
810
      }
811 812

      if (type == MMD_TYPE_NORMAL_TEXT)
813 814 815
      {
        if (lineptr[1] == '*' && !isspace(lineptr[2] & 255))
        {
816 817
          type = MMD_TYPE_STRONG_TEXT;
          lineptr ++;
818 819 820 821 822 823
        }
        else if (!isspace(lineptr[1] & 255))
        {
          type = MMD_TYPE_EMPHASIZED_TEXT;
        }

824 825 826 827 828 829
        text = lineptr + 1;
      }
      else
      {
        if (lineptr[1] == '*')
          lineptr ++;
830

831 832
        type = MMD_TYPE_NORMAL_TEXT;
      }
833
    }
834
    else if (*lineptr == '`')
835
    {
836
      if (text)
837 838
      {
        *lineptr = '\0';
839
        mmd_add(parent, type, whitespace, text, NULL);
840

841 842 843
        text       = NULL;
        whitespace = 0;
      }
844

845 846 847 848 849 850 851 852 853
      if (type == MMD_TYPE_CODE_TEXT)
      {
        type = MMD_TYPE_NORMAL_TEXT;
      }
      else
      {
        type = MMD_TYPE_CODE_TEXT;
        text = lineptr + 1;
      }
854
    }
855
    else if (!text)
856
    {
857 858 859 860 861
      if (*lineptr == '\\' && lineptr[1])
      {
       /*
        * Escaped character...
        */
862

863 864 865 866
        lineptr ++;
      }

      text = lineptr;
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    }
    else if (*lineptr == '\\' && lineptr[1])
    {
     /*
      * Escaped character...
      */

      memmove(lineptr, lineptr + 1, strlen(lineptr));
    }
  }

  if (text)
    mmd_add(parent, type, whitespace, text, NULL);
}


/*
 * 'mmd_parse_link()' - Parse a link.
 */

static char *                           /* O - End of link text */
Michael Sweet's avatar
Michael Sweet committed
888 889 890
mmd_parse_link(char       *lineptr,     /* I - Pointer into line */
               char       **text,       /* O - Text */
               char       **url)        /* O - URL */
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
{
  lineptr ++; /* skip "[" */

  *text = lineptr;
  *url  = NULL;

  while (*lineptr && *lineptr != ']')
  {
    if (*lineptr == '\"')
    {
      lineptr ++;
      while (*lineptr && *lineptr != '\"')
        lineptr ++;

      if (!*lineptr)
        return (lineptr);
    }

    lineptr ++;
  }

  if (!*lineptr)
    return (lineptr);

  *lineptr++ = '\0';

  while (isspace(*lineptr & 255))
    lineptr ++;

  if (*lineptr == '(')
  {
   /*
    * Get URL...
    */

    lineptr ++;
    *url = lineptr;

    while (*lineptr && *lineptr != ')')
    {
      if (isspace(*lineptr & 255))
        *lineptr = '\0';
      else if (*lineptr == '\"')
      {
        lineptr ++;
        while (*lineptr && *lineptr != '\"')
          lineptr ++;

        if (!*lineptr)
          return (lineptr);
      }

      lineptr ++;
    }

    *lineptr++ = '\0';
  }

  return (lineptr);
}


/*
 * 'mmd_remove()' - Remove a node from its parent.
 */

static void
mmd_remove(mmd_t *node)                 /* I - Node */
{
  if (node->parent)
  {
    if (node->prev_sibling)
      node->prev_sibling->next_sibling = node->next_sibling;
    else
      node->parent->first_child = node->next_sibling;

    if (node->next_sibling)
      node->next_sibling->prev_sibling = node->prev_sibling;
    else
      node->parent->last_child = node->prev_sibling;

    node->parent       = NULL;
    node->prev_sibling = NULL;
    node->next_sibling = NULL;
  }
}