Skip to content

Commit

Permalink
Add new config options to support old-style table of contents links.
Browse files Browse the repository at this point in the history
--with-id-anchor makes discount use `id=` instead of `a name=`, and
--with-anchor-default=C makes discount use the character C instead of
`.` for illegal characters in anchors.
  • Loading branch information
david parsons authored and david parsons committed Feb 26, 2011
1 parent b11ae91 commit acdd58b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions configure.sh
Expand Up @@ -10,6 +10,8 @@
ac_help='--enable-amalloc Enable memory allocation debugging
--with-tabstops=N Set tabstops to N characters (default is 4)
--with-dl=X Use Discount, Extra, or Both types of definition list
--with-id-anchor Use id= anchors for table-of-contents links
--with-anchor-default=C Use C instead of illegal characters in anchors
--enable-all-features Turn on all stable optional features
--shared Build shared libraries (default is static)'

Expand Down Expand Up @@ -51,6 +53,9 @@ BOTH) AC_DEFINE 'USE_EXTRA_DL' 1
*) AC_FAIL "Unknown value <$WITH_DL> for --with-dl (want 'discount', 'extra', or 'both')" ;;
esac

test "$WITH_ID_ANCHOR" && AC_DEFINE 'WITH_ID_ANCHOR' 1
AC_DEFINE 'ANCHOR_DEFAULT_CHAR' "'${WITH_ANCHOR_DEFAULT:-.}'"

AC_PROG_CC

test "$TRY_SHARED" && AC_COMPILER_PIC && AC_CC_SHLIBS
Expand Down
12 changes: 12 additions & 0 deletions generate.c
Expand Up @@ -1353,6 +1353,17 @@ text(MMIOT *f)
static void
printheader(Paragraph *pp, MMIOT *f)
{
#if WITH_ID_ANCHOR
Qprintf(f, "<h%d", pp->hnumber);
if ( f->flags & MKD_TOC ) {
Qstring(" id=\"", f);
mkd_string_to_anchor(T(pp->text->text),
S(pp->text->text),
(mkd_sta_function_t)Qchar, f, 1);
Qchar('"', f);
}
Qchar('>', f);
#else
if ( f->flags & MKD_TOC ) {
Qstring("<a name=\"", f);
mkd_string_to_anchor(T(pp->text->text),
Expand All @@ -1361,6 +1372,7 @@ printheader(Paragraph *pp, MMIOT *f)
Qstring("\"></a>\n", f);
}
Qprintf(f, "<h%d>", pp->hnumber);
#endif
push(T(pp->text->text), S(pp->text->text), f);
text(f);
Qprintf(f, "</h%d>", pp->hnumber);
Expand Down
2 changes: 1 addition & 1 deletion mkdio.c
Expand Up @@ -233,7 +233,7 @@ mkd_string_to_anchor(char *s, int len, mkd_sta_function_t outchar,
if ( isalnum(c) || (c == '_') || (c == ':') || (c == '-') || (c == '.' ) )
(*outchar)(c, out);
else
(*outchar)('.',out);
(*outchar)(ANCHOR_DEFAULT_CHAR, out);
}
else
(*outchar)(c,out);
Expand Down
5 changes: 5 additions & 0 deletions tests/toc.t
@@ -1,5 +1,10 @@
. tests/functions.sh

# don't bother to test the table of contents code
# if we're using id= for labelling things
#
./markdown -V | grep ID-ANCHOR >/dev/null && exit 0

title "table-of-contents support"

rc=0
Expand Down
4 changes: 3 additions & 1 deletion version.c.in
Expand Up @@ -18,5 +18,7 @@ char markdown_version[] = VERSION
#else
" DL=NONE"
#endif

#if WITH_ID_ANCHOR
" ID-ANCHOR"
#endif
;

0 comments on commit acdd58b

Please sign in to comment.