| 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 |
|
| 21 |
|
| 22 |
static char * |
| 23 |
mkd_xmlchar(unsigned char c) |
| 24 |
{ |
| 25 |
switch (c) { |
| 26 |
case '<': return "<"; |
| 27 |
case '>': return ">"; |
| 28 |
case '&': return "&"; |
| 29 |
case '"': return """; |
| 30 |
case '\'': return "'"; |
| 31 |
default: if ( isascii(c) || (c & 0x80) ) |
| 32 |
return 0; |
| 33 |
return ""; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
int |
| 41 |
mkd_generatexml(char *p, int size, FILE *out) |
| 42 |
{ |
| 43 |
unsigned char c; |
| 44 |
char *entity; |
| 45 |
|
| 46 |
while ( size-- > 0 ) { |
| 47 |
c = *p++; |
| 48 |
|
| 49 |
if ( entity = mkd_xmlchar(c) ) |
| 50 |
DO_OR_DIE( fputs(entity, out) ); |
| 51 |
else |
| 52 |
DO_OR_DIE( fputc(c, out) ); |
| 53 |
} |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
int |
| 61 |
mkd_xml(char *p, int size, char **res) |
| 62 |
{ |
| 63 |
unsigned char c; |
| 64 |
char *entity; |
| 65 |
Cstring f; |
| 66 |
|
| 67 |
CREATE(f); |
| 68 |
RESERVE(f, 100); |
| 69 |
|
| 70 |
while ( size-- > 0 ) { |
| 71 |
c = *p++; |
| 72 |
if ( entity = mkd_xmlchar(c) ) |
| 73 |
Cswrite(&f, entity, strlen(entity)); |
| 74 |
else |
| 75 |
Csputc(c, &f); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
EXPAND(f) = 0; |
| 81 |
*res = strdup(T(f)); |
| 82 |
return S(f)-1; |
| 83 |
} |