"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "mkdio.c" between
discount-2.2.4.tar.bz2 and discount-2.2.6.tar.bz2

About: Discount is an implementation of the "Markdown" markup language (written in C).

mkdio.c  (discount-2.2.4.tar.bz2):mkdio.c  (discount-2.2.6.tar.bz2)
skipping to change at line 79 skipping to change at line 79
EXPAND(p->text) = 0; EXPAND(p->text) = 0;
S(p->text)--; S(p->text)--;
p->dle = mkd_firstnonblank(p); p->dle = mkd_firstnonblank(p);
} }
/* trim leading characters from a line, then adjust the dle. /* trim leading characters from a line, then adjust the dle.
*/ */
void void
__mkd_trim_line(Line *p, int clip) __mkd_trim_line(Line *p, int clip)
{ {
if ( clip >= S(p->text) ) if ( clip >= S(p->text) ) {
S(p->text) = p->dle = 0; S(p->text) = p->dle = 0;
T(p->text)[0] = 0;
}
else if ( clip > 0 ) { else if ( clip > 0 ) {
CLIP(p->text, 0, clip); CLIP(p->text, 0, clip);
p->dle = mkd_firstnonblank(p); p->dle = mkd_firstnonblank(p);
} }
} }
/* build a Document from any old input. /* build a Document from any old input.
*/ */
typedef int (*getc_func)(void*); typedef int (*getc_func)(void*);
Document * Document *
populate(getc_func getc, void* ctx, int flags) populate(getc_func getc, void* ctx, mkd_flag_t flags)
{ {
Cstring line; Cstring line;
Document *a = __mkd_new_Document(); Document *a = __mkd_new_Document();
int c; int c;
int pandoc = 0; int pandoc = 0;
if ( !a ) return 0; if ( !a ) return 0;
a->tabstop = (flags & MKD_TABSTOP) ? 4 : TABSTOP; a->tabstop = is_flag_set(flags, MKD_TABSTOP) ? 4 : TABSTOP;
CREATE(line); CREATE(line);
while ( (c = (*getc)(ctx)) != EOF ) { while ( (c = (*getc)(ctx)) != EOF ) {
if ( c == '\n' ) { if ( c == '\n' ) {
if ( pandoc != EOF && pandoc < 3 ) { if ( pandoc != EOF && pandoc < 3 ) {
if ( S(line) && (T(line)[0] == '%') ) if ( S(line) && (T(line)[0] == '%') )
pandoc++; pandoc++;
else else
pandoc = EOF; pandoc = EOF;
skipping to change at line 125 skipping to change at line 127
} }
else if ( isprint(c) || isspace(c) || (c & 0x80) ) else if ( isprint(c) || isspace(c) || (c & 0x80) )
EXPAND(line) = c; EXPAND(line) = c;
} }
if ( S(line) ) if ( S(line) )
__mkd_enqueue(a, &line); __mkd_enqueue(a, &line);
DELETE(line); DELETE(line);
if ( (pandoc == 3) && !(flags & (MKD_NOHEADER|MKD_STRICT)) ) { if ( (pandoc == 3) && !(is_flag_set(flags, MKD_NOHEADER) || is_flag_set(flag s, MKD_STRICT)) ) {
/* the first three lines started with %, so we have a header. /* the first three lines started with %, so we have a header.
* clip the first three lines out of content and hang them * clip the first three lines out of content and hang them
* off header. * off header.
*/ */
Line *headers = T(a->content); Line *headers = T(a->content);
a->title = headers; __mkd_trim_line(a->title, 1); a->title = headers; __mkd_trim_line(a->title, 1);
a->author= headers->next; __mkd_trim_line(a->author, 1); a->author= headers->next; __mkd_trim_line(a->author, 1);
a->date = headers->next->next; __mkd_trim_line(a->date, 1); a->date = headers->next->next; __mkd_trim_line(a->date, 1);
T(a->content) = headers->next->next->next; T(a->content) = headers->next->next->next;
} }
return a; return a;
} }
/* convert a file into a linked list /* convert a file into a linked list
*/ */
Document * Document *
mkd_in(FILE *f, DWORD flags) mkd_in(FILE *f, mkd_flag_t flags)
{ {
return populate((getc_func)fgetc, f, flags & INPUT_MASK); return populate((getc_func)fgetc, f, flags & INPUT_MASK);
} }
/* return a single character out of a buffer /* return a single character out of a buffer
*/ */
int int
__mkd_io_strget(struct string_stream *in) __mkd_io_strget(struct string_stream *in)
{ {
if ( !in->size ) return EOF; if ( !in->size ) return EOF;
--(in->size); --(in->size);
return *(in->data)++; return *(in->data)++;
} }
/* convert a block of text into a linked list /* convert a block of text into a linked list
*/ */
Document * Document *
mkd_string(const char *buf, int len, DWORD flags) mkd_string(const char *buf, int len, mkd_flag_t flags)
{ {
struct string_stream about; struct string_stream about;
about.data = buf; about.data = buf;
about.size = len; about.size = len;
return populate((getc_func)__mkd_io_strget, &about, flags & INPUT_MASK); return populate((getc_func)__mkd_io_strget, &about, flags & INPUT_MASK);
} }
/* write the html to a file (xmlified if necessary) /* write the html to a file (xmlified if necessary)
*/ */
int int
mkd_generatehtml(Document *p, FILE *output) mkd_generatehtml(Document *p, FILE *output)
{ {
char *doc; char *doc;
int szdoc; int szdoc;
DO_OR_DIE( szdoc = mkd_document(p,&doc) ); DO_OR_DIE( szdoc = mkd_document(p,&doc) );
if ( p->ctx->flags & MKD_CDATA ) if ( is_flag_set(p->ctx->flags, MKD_CDATA) )
DO_OR_DIE( mkd_generatexml(doc, szdoc, output) ); DO_OR_DIE( mkd_generatexml(doc, szdoc, output) );
else if ( fwrite(doc, szdoc, 1, output) != 1 ) else if ( fwrite(doc, szdoc, 1, output) != 1 )
return EOF; return EOF;
DO_OR_DIE( putc('\n', output) ); DO_OR_DIE( putc('\n', output) );
return 0; return 0;
} }
/* convert some markdown text to html /* convert some markdown text to html
*/ */
int int
markdown(Document *document, FILE *out, int flags) markdown(Document *document, FILE *out, mkd_flag_t flags)
{ {
if ( mkd_compile(document, flags) ) { if ( mkd_compile(document, flags) ) {
mkd_generatehtml(document, out); mkd_generatehtml(document, out);
mkd_cleanup(document); mkd_cleanup(document);
return 0; return 0;
} }
return -1; return -1;
} }
/* write out a Cstring, mangled into a form suitable for `<a href=` or `<a id=` /* anchor_format a string, returning the formatted string in malloc()ed space
* MKD_URLENCODEDANCHOR is now perverted to being a html5 anchor
*
* !labelformat: print all characters
* labelformat && h4anchor: prefix nonalpha label with L,
* expand all nonalnum, _, ':', '.' to hex
* except space which maps to -
* labelformat && !h4anchor:expand space to -, other isspace() & '%' to hex
*/ */
void static char *
mkd_string_to_anchor(char *s, int len, mkd_sta_function_t outchar, mkd_anchor_format(char *s, int len, int labelformat, mkd_flag_t flags)
void *out, int labelformat,
DWORD flags)
{ {
static const unsigned char hexchars[] = "0123456789abcdef"; char *res;
unsigned char c; unsigned char c;
int i, needed, out = 0;
int h4anchor = !is_flag_set(flags, MKD_URLENCODEDANCHOR);
static const unsigned char hexchars[] = "0123456789abcdef";
int i, size; needed = labelformat ? (4*len) : len;
char *line;
/* MKD_URLENCODEDANCHOR is now perverted to being a html5 anchor
*
* !labelformat: print all characters
* labelformat && h4anchor: prefix nonalpha label with L,
* expand all nonalnum, _, ':', '.' to hex
* except space which maps to -
* labelformat && !h4anchor:expand space to -, other isspace() & '%' to hex
*/
int h4anchor = !(flags & MKD_URLENCODEDANCHOR);
size = mkd_line(s, len, &line, IS_LABEL); if ( (res = malloc(needed)) == NULL )
return NULL;
if ( h4anchor && labelformat && (size>0) && !isalpha(line[0]) ) if ( h4anchor && labelformat && !isalpha(s[0]) )
(*outchar)('L',out); res[out++] = 'L';
for ( i=0; i < size ; i++ ) { for ( i=0; i < len ; i++ ) {
c = line[i]; c = s[i];
if ( labelformat ) { if ( labelformat ) {
if ( h4anchor if ( h4anchor
? (isalnum(c) || (c == '_') || (c == ':') || (c == '.' ) ) ? (isalnum(c) || (c == '_') || (c == ':') || (c == '.' ) )
: !(isspace(c) || c == '%') ) : !(isspace(c) || c == '%') )
(*outchar)(c, out); res[out++] = c;
else if ( c == ' ' )
res[out++] = '-';
else { else {
if ( c == ' ' ) res[out++] = h4anchor ? '-' : '%';
(*outchar)('-', out); res[out++] = hexchars[c >> 4 & 0xf];
else { res[out++] = hexchars[c & 0xf];
(*outchar)(h4anchor ? '-' : '%', out);
(*outchar)(hexchars[c >> 4 & 0xf], out);
(*outchar)(hexchars[c & 0xf], out);
if ( h4anchor ) if ( h4anchor )
(*outchar)('-', out); res[out++] = '-';
}
} }
} }
else else
(*outchar)(c,out); res[out++] = c;
} }
if (line) res[out++] = 0;
free(line); return res;
} /* mkd_anchor_format */
/* write out a Cstring, mangled into a form suitable for `<a href=` or `<a id=`
*/
void
mkd_string_to_anchor(char *s, int len, mkd_sta_function_t outchar,
void *out, int labelformat,
MMIOT *f)
{
char *res;
char *line;
int size;
int i;
size = mkd_line(s, len, &line, IS_LABEL);
if ( !line )
return;
if ( f->cb->e_anchor )
res = (*(f->cb->e_anchor))(line, size, f->cb->e_data);
else
res = mkd_anchor_format(line, size, labelformat, f->flags);
free(line);
if ( !res )
return;
for ( i=0; res[i]; i++ )
(*outchar)(res[i], out);
if ( f->cb->e_anchor ) {
if ( f->cb->e_free )
(*(f->cb->e_free))(res, f->cb->e_data);
}
else
free(res);
} }
/* ___mkd_reparse() a line /* ___mkd_reparse() a line
*/ */
static void static void
mkd_parse_line(char *bfr, int size, MMIOT *f, int flags) mkd_parse_line(char *bfr, int size, MMIOT *f, mkd_flag_t flags)
{ {
___mkd_initmmiot(f, 0); ___mkd_initmmiot(f, 0);
f->flags = flags & USER_FLAGS; f->flags = flags & USER_FLAGS;
___mkd_reparse(bfr, size, 0, f, 0); ___mkd_reparse(bfr, size, 0, f, 0);
___mkd_emblock(f); ___mkd_emblock(f);
} }
/* ___mkd_reparse() a line, returning it in malloc()ed memory /* ___mkd_reparse() a line, returning it in malloc()ed memory
*/ */
int int
mkd_line(char *bfr, int size, char **res, DWORD flags) mkd_line(char *bfr, int size, char **res, mkd_flag_t flags)
{ {
MMIOT f; MMIOT f;
int len; int len;
mkd_parse_line(bfr, size, &f, flags); mkd_parse_line(bfr, size, &f, flags);
if ( len = S(f.out) ) { if ( len = S(f.out) ) {
EXPAND(f.out) = 0; EXPAND(f.out) = 0;
/* strdup() doesn't use amalloc(), so in an amalloc()ed /* strdup() doesn't use amalloc(), so in an amalloc()ed
* build this copies the string safely out of our memory * build this copies the string safely out of our memory
skipping to change at line 301 skipping to change at line 339
*res = 0; *res = 0;
len = EOF; len = EOF;
} }
___mkd_freemmiot(&f, 0); ___mkd_freemmiot(&f, 0);
return len; return len;
} }
/* ___mkd_reparse() a line, writing it to a FILE /* ___mkd_reparse() a line, writing it to a FILE
*/ */
int int
mkd_generateline(char *bfr, int size, FILE *output, DWORD flags) mkd_generateline(char *bfr, int size, FILE *output, mkd_flag_t flags)
{ {
MMIOT f; MMIOT f;
int status; int status;
mkd_parse_line(bfr, size, &f, flags); mkd_parse_line(bfr, size, &f, flags);
if ( flags & MKD_CDATA ) if ( is_flag_set(flags, MKD_CDATA) )
status = mkd_generatexml(T(f.out), S(f.out), output) != EOF; status = mkd_generatexml(T(f.out), S(f.out), output) != EOF;
else else
status = fwrite(T(f.out), S(f.out), 1, output) == S(f.out); status = fwrite(T(f.out), S(f.out), 1, output) == S(f.out);
___mkd_freemmiot(&f, 0); ___mkd_freemmiot(&f, 0);
return status ? 0 : EOF; return status ? 0 : EOF;
} }
/* set the url display callback /* set the url display callback
*/ */
skipping to change at line 340 skipping to change at line 378
void void
mkd_e_flags(Document *f, mkd_callback_t edit) mkd_e_flags(Document *f, mkd_callback_t edit)
{ {
if ( f ) { if ( f ) {
if ( f->cb.e_flags != edit ) if ( f->cb.e_flags != edit )
f->dirty = 1; f->dirty = 1;
f->cb.e_flags = edit; f->cb.e_flags = edit;
} }
} }
/* set the anchor formatter
*/
void
mkd_e_anchor(Document *f, mkd_callback_t format)
{
if ( f ) {
if ( f->cb.e_anchor != format )
f->dirty = 1;
f->cb.e_anchor = format;
}
}
/* set the url display/options deallocator /* set the url display/options deallocator
*/ */
void void
mkd_e_free(Document *f, mkd_free_t dealloc) mkd_e_free(Document *f, mkd_free_t dealloc)
{ {
if ( f ) { if ( f ) {
if ( f->cb.e_free != dealloc ) if ( f->cb.e_free != dealloc )
f->dirty = 1; f->dirty = 1;
f->cb.e_free = dealloc; f->cb.e_free = dealloc;
} }
skipping to change at line 364 skipping to change at line 414
void void
mkd_e_data(Document *f, void *data) mkd_e_data(Document *f, void *data)
{ {
if ( f ) { if ( f ) {
if ( f->cb.e_data != data ) if ( f->cb.e_data != data )
f->dirty = 1; f->dirty = 1;
f->cb.e_data = data; f->cb.e_data = data;
} }
} }
/* set the code block display callback
*/
void
mkd_e_code_format(Document *f, mkd_callback_t codefmt)
{
if ( f && (f->cb.e_codefmt != codefmt) ) {
f->dirty = 1;
f->cb.e_codefmt = codefmt;
}
}
/* set the href prefix for markdown extra style footnotes /* set the href prefix for markdown extra style footnotes
*/ */
void void
mkd_ref_prefix(Document *f, char *data) mkd_ref_prefix(Document *f, char *data)
{ {
if ( f ) { if ( f ) {
if ( f->ref_prefix != data ) if ( f->ref_prefix != data )
f->dirty = 1; f->dirty = 1;
f->ref_prefix = data; f->ref_prefix = data;
} }
 End of changes. 28 change blocks. 
46 lines changed or deleted 107 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)