Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 86 lines (74 sloc) 1.821 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/* markdown: a C implementation of John Gruber's Markdown markup language.
*
* Copyright (C) 2007 David L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#include "cstring.h"
#include "markdown.h"
#include "amalloc.h"

/* on merge: moved config.h last to avoid conflict with windows headers */
#include "config.h"

/* return the xml version of a character
*/
static char *
mkd_xmlchar(unsigned char c)
{
    switch (c) {
    case '<': return "&lt;";
    case '>': return "&gt;";
    case '&': return "&amp;";
    case '"': return "&quot;";
    case '\'': return "&apos;";
    default: if ( isascii(c) || (c & 0x80) )
return 0;
return "";
    }
}


/* write output in XML format
*/
int
mkd_generatexml(char *p, int size, FILE *out)
{
    unsigned char c;
    char *entity;
int ret = 0;

/* on merge: changed so the return value has some meaning */

    while ( size-- > 0 ) {
c = *p++;

if ( entity = mkd_xmlchar(c) )
ret |= fputs(entity, out);
else
ret |= fputc(c, out);
    }
    return ret;
}


/* build a xml'ed version of a string
*/
int
mkd_xml(char *p, int size, char **res)
{
    unsigned char c;
    char *entity;
    Cstring f;

    CREATE(f);
    RESERVE(f, 100);

    while ( size-- > 0 ) {
c = *p++;
if ( entity = mkd_xmlchar(c) )
Cswrite(&f, entity, strlen(entity));
else
Csputc(c, &f);
    }
/* HACK ALERT! HACK ALERT! HACK ALERT! */
    *res = T(f); /* we know that a T(Cstring) is a character pointer */
/* so we can simply pick it up and carry it away, */
    return S(f); /* leaving the husk of the Ctring on the stack */
/* END HACK ALERT */
}
Something went wrong with that request. Please try again.