Skip to content

Commit

Permalink
Convert --with-fenced-code to runtime flag for issue Orc#124.
Browse files Browse the repository at this point in the history
This change is more invasive and difficult for me as not all existing
preprocessor directives directly translate to `if () {}`.

Instead of passing `--with-fenced-code` to `configure.sh`, now use
`MKD_FENCEDCODE` or `-ffencedcode`.

The output of `markdown -VV` will now display `FENCEDCODE` if the
feature is enabled and `!FENCEDCODE` if it is disabled rather than
displaying `FENCED-CODE` if the feature is enabled.
  • Loading branch information
binki committed Jul 13, 2015
1 parent b09c14a commit 157dad9
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 66 deletions.
6 changes: 2 additions & 4 deletions configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ ac_help='--enable-amalloc Enable memory allocation debugging
--with-tabstops=N Set tabstops to N characters (default is 4)
--with-id-anchor Use id= anchors for table-of-contents links
--with-github-tags Allow `_` and `-` in <> tags
--with-fenced-code Allow fenced code blocks
--with-urlencoded-anchor Use url-encoded chars to multibyte chars in toc links
--enable-all-features Turn on all stable optional features
--shared Build shared libraries (default is static)'
Expand Down Expand Up @@ -47,14 +46,13 @@ TARGET=markdown

AC_INIT $TARGET

for banned_with in dl; do
banned_with_variable_ref=\$WITH_`echo "$banned_with" | $AC_UPPERCASE`
for banned_with in dl fenced-code; do
banned_with_variable_ref=\$WITH_`echo "$banned_with" | $AC_UPPERCASE | tr - _`
if [ "`eval echo "$banned_with_variable_ref"`" ]; then
AC_FAIL "Invalid option: --with-$banned_with. Please use a runtime flag to configure this feature."
fi
done

test "$WITH_FENCED_CODE" && AC_DEFINE "WITH_FENCED_CODE" 1
test "$WITH_ID_ANCHOR" && AC_DEFINE 'WITH_ID_ANCHOR' 1
test "$WITH_GITHUB_TAGS" && AC_DEFINE 'WITH_GITHUB_TAGS' 1
test "$WITH_URLENCODED_ANCHOR" && AC_DEFINE 'WITH_URLENCODED_ANCHOR' 1
Expand Down
1 change: 1 addition & 0 deletions flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static struct flagnames flagnames[] = {
{ MKD_NOSTYLE, "!STYLE" },
{ MKD_NODLDISCOUNT, "!DLDISCOUNT" },
{ MKD_DLEXTRA, "DLEXTRA" },
{ MKD_FENCEDCODE, "FENCEDCODE" },
};
#define NR(x) (sizeof x/sizeof x[0])

Expand Down
2 changes: 2 additions & 0 deletions markdown.1
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Enable extra-style definition lists (not default). Both styles may be enabled si
Allow markdown extra-style footnotes.
.It Ar style
Extract <style> blocks from the output.
.It Ar fencedcode
Allow fenced code blocks (not default).
.El
.Pp
As an example, the option
Expand Down
2 changes: 2 additions & 0 deletions markdown.3
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ Enable the extra definition list syntax style.
Enable markdown extra-style footnotes.
.It Ar MKD_NOSTYLE
Do not extract (omit) <style/> blocks from the output.
.It Ar MKD_FENCEDCODE
Allow fenced code blocks.
.El
.Sh RETURN VALUES
.Fn markdown
Expand Down
80 changes: 40 additions & 40 deletions markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,16 @@ splitline(Line *t, int cutpoint)

#define UNCHECK(l) ((l)->flags &= ~CHECKED)

#ifdef WITH_FENCED_CODE
# define UNLESS_FENCED(t) if (fenced) { \
#define UNLESS_FENCED(t) if (fenced) { \
other = 1; l->count += (c == ' ' ? 0 : -1); \
} else { t; }
#else
# define UNLESS_FENCED(t) t;
#endif

/*
* walk a line, seeing if it's any of half a dozen interesting regular
* types.
*/
static void
checkline(Line *l)
checkline(Line *l, DWORD flags)
{
int eol, i;
int dashes = 0, spaces = 0,
Expand All @@ -210,6 +206,7 @@ checkline(Line *l)

for (i=l->dle; i<eol; i++) {
register int c = T(l->text)[i];
int is_fence_char = 0;

if ( c != ' ' ) l->count++;

Expand All @@ -219,14 +216,20 @@ checkline(Line *l)
case '=': equals = 1; break;
case '_': UNLESS_FENCED(underscores = 1); break;
case '*': stars = 1; break;
#if WITH_FENCED_CODE
case '~': if (other) return; fenced = 1; tildes = 1; break;
case '`': if (other) return; fenced = 1; backticks = 1; break;
#endif
default:
other = 1;
l->count--;
if (!fenced) return;
if (flags & MKD_FENCEDCODE) {
switch (c) {
case '~': if (other) return; is_fence_char = 1; tildes = 1; break;
case '`': if (other) return; is_fence_char = 1; backticks = 1; break;
}
if (is_fence_char) {
fenced = 1;
break;
}
}
other = 1;
l->count--;
if (!fenced) return;
}
}

Expand All @@ -242,10 +245,8 @@ checkline(Line *l)
if ( stars || underscores ) { l->kind = chk_hr; }
else if ( dashes ) { l->kind = chk_dash; }
else if ( equals ) { l->kind = chk_equal; }
#if WITH_FENCED_CODE
else if ( tildes ) { l->kind = chk_tilde; }
else if ( backticks ) { l->kind = chk_backtick; }
#endif
}


Expand Down Expand Up @@ -372,10 +373,10 @@ iscode(Line *t)


static inline int
ishr(Line *t)
ishr(Line *t, DWORD flags)
{
if ( ! (t->flags & CHECKED) )
checkline(t);
checkline(t, flags);

if ( t->count > 2 )
return t->kind == chk_hr || t->kind == chk_dash || t->kind == chk_equal;
Expand All @@ -384,7 +385,7 @@ ishr(Line *t)


static int
issetext(Line *t, int *htyp)
issetext(Line *t, int *htyp, DWORD flags)
{
Line *n;

Expand All @@ -394,7 +395,7 @@ issetext(Line *t, int *htyp)

if ( (n = t->next) ) {
if ( !(n->flags & CHECKED) )
checkline(n);
checkline(n, flags);

if ( n->kind == chk_dash || n->kind == chk_equal ) {
*htyp = SETEXT;
Expand All @@ -406,7 +407,7 @@ issetext(Line *t, int *htyp)


static int
ishdr(Line *t, int *htyp)
ishdr(Line *t, int *htyp, DWORD flags)
{
/* ANY leading `#`'s make this into an ETX header
*/
Expand All @@ -417,19 +418,19 @@ ishdr(Line *t, int *htyp)

/* And if not, maybe it's a SETEXT header instead
*/
return issetext(t, htyp);
return issetext(t, htyp, flags);
}


static inline int
end_of_block(Line *t)
end_of_block(Line *t, DWORD flags)
{
int dummy;

if ( !t )
return 0;

return ( (S(t->text) <= t->dle) || ishr(t) || ishdr(t, &dummy) );
return ( (S(t->text) <= t->dle) || ishr(t, flags) || ishdr(t, &dummy, flags) );
}


Expand Down Expand Up @@ -471,7 +472,7 @@ is_extra_dt(Line *t, int *clip, DWORD flags)
&& T(t->text)[S(t->text)-1] != '=') {
Line *x;

if ( iscode(t) || end_of_block(t) )
if ( iscode(t) || end_of_block(t, flags) )
return 0;

if ( (x = skipempty(t->next)) && is_extra_dd(x) ) {
Expand Down Expand Up @@ -506,7 +507,7 @@ islist(Line *t, int *clip, DWORD flags, int *list_type)
int i, j;
char *q;

if ( end_of_block(t) )
if ( end_of_block(t, flags) )
return 0;

if ( !(flags & (MKD_NODLIST|MKD_STRICT)) && isdefinition(t,clip,list_type,flags) )
Expand Down Expand Up @@ -614,12 +615,14 @@ codeblock(Paragraph *p)
}


#ifdef WITH_FENCED_CODE
static int
iscodefence(Line *r, int size, line_type kind)
iscodefence(Line *r, int size, line_type kind, DWORD flags)
{
if ( !(flags & MKD_FENCEDCODE) )
return 0;

if ( !(r->flags & CHECKED) )
checkline(r);
checkline(r, flags);

if ( kind )
return (r->kind == kind) && (r->count >= size);
Expand All @@ -628,7 +631,7 @@ iscodefence(Line *r, int size, line_type kind)
}

static Paragraph *
fencedcodeblock(ParagraphRoot *d, Line **ptr)
fencedcodeblock(ParagraphRoot *d, Line **ptr, DWORD flags)
{
Line *first, *r;
Paragraph *ret;
Expand All @@ -637,14 +640,14 @@ fencedcodeblock(ParagraphRoot *d, Line **ptr)

/* don't allow zero-length code fences
*/
if ( (first->next == 0) || iscodefence(first->next, first->count, 0) )
if ( (first->next == 0) || iscodefence(first->next, first->count, 0, flags) )
return 0;

/* find the closing fence, discard the fences,
* return a Paragraph with the contents
*/
for ( r = first; r && r->next; r = r->next )
if ( iscodefence(r->next, first->count, first->kind) ) {
if ( iscodefence(r->next, first->count, first->kind, flags) ) {
(*ptr) = r->next->next;
ret = Pp(d, first->next, CODE);
if (S(first->text) - first->count > 0) {
Expand All @@ -662,7 +665,6 @@ fencedcodeblock(ParagraphRoot *d, Line **ptr)
}
return 0;
}
#endif


static int
Expand All @@ -688,7 +690,7 @@ endoftextblock(Line *t, int toplevelblock, DWORD flags)
{
int z;

if ( end_of_block(t) || isquote(t) )
if ( end_of_block(t, flags) || isquote(t) )
return 1;

/* HORRIBLE STANDARDS KLUDGES:
Expand Down Expand Up @@ -877,9 +879,9 @@ listitem(Paragraph *p, int indent, DWORD flags, linefn check)
indent = clip ? clip : 2;
}

if ( (q->dle < indent) && (ishr(q) || islist(q,&z,flags,&z)
if ( (q->dle < indent) && (ishr(q,flags) || islist(q,&z,flags,&z)
|| (check && (*check)(q)))
&& !issetext(q,&z) ) {
&& !issetext(q,&z,flags) ) {
q = t->next;
t->next = 0;
return q;
Expand Down Expand Up @@ -1253,11 +1255,9 @@ compile(Line *ptr, int toplevel, MMIOT *f)

ptr = codeblock(p);
}
#if WITH_FENCED_CODE
else if ( iscodefence(ptr,3,0) && (p=fencedcodeblock(&d, &ptr)) )
else if ( iscodefence(ptr,3,0,f->flags) && (p=fencedcodeblock(&d, &ptr, f->flags)) )
/* yay, it's already done */ ;
#endif
else if ( ishr(ptr) ) {
else if ( ishr(ptr, f->flags) ) {
p = Pp(&d, 0, HR);
r = ptr;
ptr = ptr->next;
Expand All @@ -1279,7 +1279,7 @@ compile(Line *ptr, int toplevel, MMIOT *f)
p->down = compile(p->text, 1, f);
p->text = 0;
}
else if ( ishdr(ptr, &hdr_type) ) {
else if ( ishdr(ptr, &hdr_type, f->flags) ) {
p = Pp(&d, ptr, HDR);
ptr = headerblock(p, hdr_type);
}
Expand Down
1 change: 1 addition & 0 deletions markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ typedef struct mmiot {
#define MKD_NOSTYLE 0x00400000
#define MKD_NODLDISCOUNT 0x00800000
#define MKD_DLEXTRA 0x01000000
#define MKD_FENCEDCODE 0x02000000
#define IS_LABEL 0x08000000
#define USER_FLAGS 0x0FFFFFFF
#define INPUT_MASK (MKD_NOHEADER|MKD_TABSTOP)
Expand Down
1 change: 1 addition & 0 deletions mkdio.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void mkd_ref_prefix(MMIOT*, char*);
#define MKD_NOSTYLE 0x00400000 /* don't extract <style> blocks */
#define MKD_NODLDISCOUNT 0x00800000 /* disable discount-style definition lists */
#define MKD_DLEXTRA 0x01000000 /* enable extra-style definition lists */
#define MKD_FENCEDCODE 0x02000000 /* enabled fenced code blocks */

#define MKD_EMBED MKD_NOLINKS|MKD_NOIMAGE|MKD_TAGTEXT

Expand Down
1 change: 1 addition & 0 deletions pgm_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static struct _opt {
{ "style", "extract style blocks", 1, 0, 1, MKD_NOSTYLE },
{ "dldiscount", "discount-style definition lists", 1, 0, 1, MKD_NODLDISCOUNT },
{ "dlextra", "extra-style definition lists", 0, 0, 1, MKD_DLEXTRA },
{ "fencedcode", "fenced code blocks", 0, 0, 1, MKD_FENCEDCODE },
} ;

#define NR(x) (sizeof x / sizeof x[0])
Expand Down
Loading

0 comments on commit 157dad9

Please sign in to comment.