Skip to content

Commit

Permalink
bring discount sources up to v1.6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Jun 15, 2010
1 parent 5efa97d commit dc18b3d
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 101 deletions.
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ end
CLEAN.include 'ext/Makefile', 'ext/mkmf.log'

file "ext/rdiscount.#{DLEXT}" => FileList["ext/Makefile"] do |f|
sh 'cd ext && make clean && make'
sh 'cd ext && make clean && make && rm -rf conftest.dSYM'
end
CLEAN.include 'ext/*.{o,bundle,so,dll}'

Expand Down Expand Up @@ -112,8 +112,8 @@ desc 'Gather required discount sources into extension directory'
task :gather => 'discount' do |t|
files =
FileList[
'discount/{markdown,mkdio,amalloc,cstring}.h',
'discount/{markdown,docheader,dumptree,generate,mkdio,resource,toc,Csio,xml,css,basename,emmatch}.c'
'discount/{markdown,mkdio,amalloc,cstring,tags}.h',
'discount/{markdown,docheader,dumptree,generate,mkdio,resource,toc,Csio,xml,css,basename,emmatch,tags,html5}.c'
]
cp files, 'ext/',
:preserve => true,
Expand Down
4 changes: 3 additions & 1 deletion ext/cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <string.h>
#include <stdlib.h>

#include "amalloc.h"
#ifndef __WITHOUT_AMALLOC
# include "amalloc.h"
#endif

/* expandable Pascal-style string.
*/
Expand Down
1 change: 1 addition & 0 deletions ext/dumptree.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Pptype(int typ)
case HR : return "hr";
case TABLE : return "table";
case SOURCE : return "source";
case STYLE : return "style";
default : return "mystery node!";
}
}
Expand Down
3 changes: 2 additions & 1 deletion ext/generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,10 @@ static linkytype linkt = { 0, 0, "<a href=\"", "\"",
*/
static linkytype specials[] = {
{ "id:", 3, "<a id=\"", "\"", 0, ">", "</a>", 0, IS_URL },
{ "class:", 6, "<span class=\"", "\"", 0, ">", "</span>", 0, 0 },
{ "raw:", 4, 0, 0, 0, 0, 0, DENY_HTML, 0 },
{ "lang:", 5, "<span lang=\"", "\"", 0, ">", "</span>", 0, 0 },
{ "abbr:", 5, "<abbr title=\"", "\"", 0, ">", "</abbr>", 0, 0 },
{ "class:", 6, "<span class=\"", "\"", 0, ">", "</span>", 0, 0 },
} ;

#define NR(x) (sizeof x / sizeof x[0])
Expand Down
24 changes: 24 additions & 0 deletions ext/html5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* block-level tags for passing html5 blocks through the blender
*/
#include "tags.h"

void
mkd_with_html5_tags()
{
static int populated = 0;

if ( populated ) return;
populated = 1;

mkd_prepare_tags();

mkd_define_tag("ASIDE", 0);
mkd_define_tag("FOOTER", 0);
mkd_define_tag("HEADER", 0);
mkd_define_tag("HGROUP", 0);
mkd_define_tag("NAV", 0);
mkd_define_tag("SECTION", 0);
mkd_define_tag("ARTICLE", 0);

mkd_sort_tags();
}
186 changes: 92 additions & 94 deletions ext/markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,24 @@
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#include "config.h"

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

/* block-level tags for passing html blocks through the blender
*/
struct kw {
char *id;
int size;
int selfclose;
} ;

#define KW(x) { x, sizeof(x)-1, 0 }
#define SC(x) { x, sizeof(x)-1, 1 }

static struct kw blocktags[] = { KW("!--"), KW("STYLE"), KW("SCRIPT"),
KW("ADDRESS"), KW("BDO"), KW("BLOCKQUOTE"),
KW("CENTER"), KW("DFN"), KW("DIV"), KW("H1"),
KW("H2"), KW("H3"), KW("H4"), KW("H5"),
KW("H6"), KW("LISTING"), KW("NOBR"),
KW("UL"), KW("P"), KW("OL"), KW("DL"),
KW("PLAINTEXT"), KW("PRE"), KW("TABLE"),
KW("WBR"), KW("XMP"), SC("HR"), SC("BR"),
KW("IFRAME"), KW("MAP"), KW("ARTICLE"),
KW("ASIDE"), KW("FOOTER"), KW("HEADER"),
KW("HGROUP"), KW("NAV"), KW("SECTION") };
#define SZTAGS (sizeof blocktags / sizeof blocktags[0])
#define MAXTAG 11 /* sizeof "BLOCKQUOTE" */
#include "tags.h"

typedef int (*stfu)(const void*,const void*);

typedef ANCHOR(Paragraph) ParagraphRoot;


/* case insensitive string sort (for qsort() and bsearch() of block tags)
*/
static int
casort(struct kw *a, struct kw *b)
{
if ( a->size != b->size )
return a->size - b->size;
return strncasecmp(a->id, b->id, b->size);
}


/* case insensitive string sort for Footnote tags.
*/
int
Expand Down Expand Up @@ -137,19 +101,28 @@ ___mkd_tidy(Cstring *t)
}


static struct kw comment = { "!--", 3, 0 };

static struct kw *
isopentag(Line *p)
{
int i=0, len;
struct kw key, *ret;
char *line;

if ( !p ) return 0;

line = T(p->text);
len = S(p->text);

if ( len < 3 || T(p->text)[0] != '<' )
if ( len < 3 || line[0] != '<' )
return 0;

if ( line[1] == '!' && line[2] == '-' && line[3] == '-' )
/* comments need special case handling, because
* the !-- doesn't need to end in a whitespace
*/
return &comment;

/* find how long the tag is so we can check to see if
* it's a block-level tag
*/
Expand All @@ -158,13 +131,8 @@ isopentag(Line *p)
&& !isspace(T(p->text)[i]); ++i )
;

key.id = T(p->text)+1;
key.size = i-1;

if ( ret = bsearch(&key, blocktags, SZTAGS, sizeof key, (stfu)casort))
return ret;

return 0;
return mkd_search_tags(T(p->text)+1, i-1);
}


Expand All @@ -173,6 +141,8 @@ typedef struct _flo {
int i;
} FLO;

#define floindex(x) (x.i)


static int
flogetc(FLO *f)
Expand All @@ -188,6 +158,41 @@ flogetc(FLO *f)
}


static void
splitline(Line *t, int cutpoint)
{
if ( t && (cutpoint < S(t->text)) ) {
Line *tmp = calloc(1, sizeof *tmp);

tmp->next = t->next;
t->next = tmp;

tmp->dle = t->dle;
SUFFIX(tmp->text, T(t->text)+cutpoint, S(t->text)-cutpoint);
S(t->text) = cutpoint;
}
}


static Line *
commentblock(Paragraph *p)
{
Line *t, *ret;
char *end;

for ( t = p->text; t ; t = t->next) {
if ( end = strstr(T(t->text), "-->") ) {
splitline(t, 3 + (end - T(t->text)) );
ret = t->next;
t->next = 0;
return ret;
}
}
return t;

}


static Line *
htmlblock(Paragraph *p, struct kw *tag)
{
Expand All @@ -196,7 +201,10 @@ htmlblock(Paragraph *p, struct kw *tag)
int c;
int i, closing, depth=0;

if ( tag->selfclose || (tag->size >= MAXTAG) ) {
if ( tag == &comment )
return commentblock(p);

if ( tag->selfclose ) {
ret = f.t->next;
f.t->next = 0;
return ret;
Expand Down Expand Up @@ -234,6 +242,7 @@ htmlblock(Paragraph *p, struct kw *tag)
}
if ( !f.t )
return 0;
splitline(f.t, floindex(f));
ret = f.t->next;
f.t->next = 0;
return ret;
Expand All @@ -246,23 +255,6 @@ htmlblock(Paragraph *p, struct kw *tag)
}


static Line *
comment(Paragraph *p)
{
Line *t, *ret;

for ( t = p->text; t ; t = t->next) {
if ( strstr(T(t->text), "-->") ) {
ret = t->next;
t->next = 0;
return ret;
}
}
return t;

}


/* tables look like
* header|header{|header}
* ------|------{|......}
Expand Down Expand Up @@ -365,26 +357,9 @@ ishr(Line *t)


static int
ishdr(Line *t, int *htyp)
issetext(Line *t, int *htyp)
{
int i;


/* first check for etx-style ###HEADER###
*/

/* leading run of `#`'s ?
*/
for ( i=0; T(t->text)[i] == '#'; ++i)
;

/* ANY leading `#`'s make this into an ETX header
*/
if ( i && (i < S(t->text) || i > 1) ) {
*htyp = ETX;
return 1;
}

/* then check for setext-style HEADER
* ======
*/
Expand All @@ -409,6 +384,31 @@ ishdr(Line *t, int *htyp)
}


static int
ishdr(Line *t, int *htyp)
{
int i;


/* first check for etx-style ###HEADER###
*/

/* leading run of `#`'s ?
*/
for ( i=0; T(t->text)[i] == '#'; ++i)
;

/* ANY leading `#`'s make this into an ETX header
*/
if ( i && (i < S(t->text) || i > 1) ) {
*htyp = ETX;
return 1;
}

return issetext(t, htyp);
}


static int
isdefinition(Line *t)
{
Expand Down Expand Up @@ -743,11 +743,12 @@ listitem(Paragraph *p, int indent)
t->next = 0;
return q;
}
/* indent as far as the initial line was indented. */
indent = clip;
/* indent at least 2, and at most as
* as far as the initial line was indented. */
indent = clip ? clip : 2;
}

if ( (q->dle < indent) && (ishr(q) || islist(q,&z)) && !ishdr(q,&z) ) {
if ( (q->dle < indent) && (ishr(q) || islist(q,&z)) && !issetext(q,&z) ) {
q = t->next;
t->next = 0;
return q;
Expand Down Expand Up @@ -948,10 +949,7 @@ compile_document(Line *ptr, MMIOT *f)
T(source) = E(source) = 0;
}
p = Pp(&d, ptr, strcmp(tag->id, "STYLE") == 0 ? STYLE : HTML);
if ( strcmp(tag->id, "!--") == 0 )
ptr = comment(p);
else
ptr = htmlblock(p, tag);
ptr = htmlblock(p, tag);
}
else if ( isfootnote(ptr) ) {
/* footnotes, like cats, sleep anywhere; pull them
Expand Down Expand Up @@ -1054,15 +1052,15 @@ compile(Line *ptr, int toplevel, MMIOT *f)
}


static void
initialize()
void
mkd_initialize()
{
static int first = 1;

if ( first-- > 0 ) {
first = 0;
INITRNG(time(0));
qsort(blocktags, SZTAGS, sizeof blocktags[0], (stfu)casort);
mkd_prepare_tags();
}
}

Expand Down Expand Up @@ -1092,7 +1090,7 @@ mkd_compile(Document *doc, int flags)
doc->ctx->footnotes = malloc(sizeof doc->ctx->footnotes[0]);
CREATE(*doc->ctx->footnotes);

initialize();
mkd_initialize();

doc->code = compile_document(T(doc->content), doc->ctx);
qsort(T(*doc->ctx->footnotes), S(*doc->ctx->footnotes),
Expand Down
Loading

0 comments on commit dc18b3d

Please sign in to comment.