Skip to content

Commit

Permalink
Convert urlencodedanchor to a runtime flag for issue Orc#124.
Browse files Browse the repository at this point in the history
Instead of `./configure.sh --with-urlencoded-anchor`, now use
`-furlencodedanchor` or `MKD_URLENCODEDANCHOR`.
  • Loading branch information
binki committed Jul 13, 2015
1 parent 5790b66 commit 5da3388
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 22 deletions.
4 changes: 1 addition & 3 deletions configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#
ac_help='--enable-amalloc Enable memory allocation debugging
--with-tabstops=N Set tabstops to N characters (default is 4)
--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 @@ -44,14 +43,13 @@ TARGET=markdown

AC_INIT $TARGET

for banned_with in dl fenced-code id-anchor github-tags; do
for banned_with in dl fenced-code id-anchor github-tags urlencoded-anchor; 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_URLENCODED_ANCHOR" && AC_DEFINE 'WITH_URLENCODED_ANCHOR' 1
test "$DEBIAN_GLITCH" && AC_DEFINE 'DEBIAN_GLITCH' 1

AC_PROG_CC
Expand Down
1 change: 1 addition & 0 deletions flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static struct flagnames flagnames[] = {
{ MKD_FENCEDCODE, "FENCEDCODE" },
{ MKD_IDANCHOR, "IDANCHOR" },
{ MKD_GITHUBTAGS, "GITHUBTAGS" },
{ MKD_URLENCODEDANCHOR, "URLENCODEDANCHOR" },
};
#define NR(x) (sizeof x/sizeof x[0])

Expand Down
4 changes: 2 additions & 2 deletions generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ printheader(Paragraph *pp, MMIOT *f)
Qstring(" id=\"", f);
mkd_string_to_anchor(T(pp->text->text),
S(pp->text->text),
(mkd_sta_function_t)Qchar, f, 1);
(mkd_sta_function_t)Qchar, f, 1, f->flags);
Qchar('"', f);
}
Qchar('>', f);
Expand All @@ -1439,7 +1439,7 @@ printheader(Paragraph *pp, MMIOT *f)
Qstring("<a name=\"", f);
mkd_string_to_anchor(T(pp->text->text),
S(pp->text->text),
(mkd_sta_function_t)Qchar, f, 1);
(mkd_sta_function_t)Qchar, f, 1, f->flags);
Qstring("\"></a>\n", f);
}
Qprintf(f, "<h%d>", pp->hnumber);
Expand Down
2 changes: 2 additions & 0 deletions markdown.1
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ Allow fenced code blocks (not default).
Use id= anchors for table-of-contents links instead of <a name=/> (not default).
.It Ar githubtags
Allow underscore and dash in passed through element names (not default).
.It Ar urlencodedanchor
Use url-encoded chars for multibyte and nonalphanumeric chars rather than dots in toc links.
.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 @@ -118,6 +118,8 @@ Allow fenced code blocks.
Use id= anchors instead of <a name=/> for table-of-contents links.
.It Ar MKD_GITHUBTAGS
Allow underscore and dash in passed through element names.
.It Ar MKD_URLENCODEDANCHOR
Use url-encoded chars for multibyte and nonalphanumeric chars rather than dots in toc links.
.El
.Sh RETURN VALUES
.Fn markdown
Expand Down
7 changes: 4 additions & 3 deletions markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ typedef struct mmiot {
#define MKD_FENCEDCODE 0x02000000
#define MKD_IDANCHOR 0x04000000
#define MKD_GITHUBTAGS 0x08000000
#define IS_LABEL 0x40000000
#define USER_FLAGS 0x7FFFFFFF
#define MKD_URLENCODEDANCHOR 0x10000000
#define IS_LABEL 0x20000000
#define USER_FLAGS 0x3FFFFFFF
#define INPUT_MASK (MKD_NOHEADER|MKD_TABSTOP)

Callback_data *cb;
Expand Down Expand Up @@ -195,7 +196,7 @@ extern int mkd_generateline(char *, int, FILE*, DWORD);
extern void mkd_basename(Document*, char *);

typedef int (*mkd_sta_function_t)(const int,const void*);
extern void mkd_string_to_anchor(char*,int, mkd_sta_function_t, void*, int);
extern void mkd_string_to_anchor(char*,int, mkd_sta_function_t, void*, int, DWORD);

extern Document *mkd_in(FILE *, DWORD);
extern Document *mkd_string(const char*,int, DWORD);
Expand Down
20 changes: 8 additions & 12 deletions mkdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,37 +213,33 @@ markdown(Document *document, FILE *out, int flags)
*/
void
mkd_string_to_anchor(char *s, int len, mkd_sta_function_t outchar,
void *out, int labelformat)
void *out, int labelformat,
DWORD flags)
{
#if WITH_URLENCODED_ANCHOR
static const unsigned char hexchars[] = "0123456789abcdef";
#endif
unsigned char c;

int i, size;
char *line;

size = mkd_line(s, len, &line, IS_LABEL);

#if !WITH_URLENCODED_ANCHOR
if ( labelformat && (size>0) && !isalpha(line[0]) )

if ( !(flags & MKD_URLENCODEDANCHOR)
&& labelformat
&& (size>0) && !isalpha(line[0]) )
(*outchar)('L',out);
#endif
for ( i=0; i < size ; i++ ) {
c = line[i];
if ( labelformat ) {
if ( isalnum(c) || (c == '_') || (c == ':') || (c == '-') || (c == '.' ) )
(*outchar)(c, out);
else
#if WITH_URLENCODED_ANCHOR
{
else if ( flags & MKD_URLENCODEDANCHOR ) {
(*outchar)('%', out);
(*outchar)(hexchars[c >> 4 & 0xf], out);
(*outchar)(hexchars[c & 0xf], out);
}
#else
else
(*outchar)('.', out);
#endif
}
else
(*outchar)(c,out);
Expand Down
1 change: 1 addition & 0 deletions mkdio.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void mkd_ref_prefix(MMIOT*, char*);
#define MKD_FENCEDCODE 0x02000000 /* enabled fenced code blocks */
#define MKD_IDANCHOR 0x04000000 /* use id= anchors for TOC links */
#define MKD_GITHUBTAGS 0x08000000 /* allow dash and underscore in element names */
#define MKD_URLENCODEDANCHOR 0x10000000 /* urlencode non-identifier chars instead of replacing with dots */

#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 @@ -60,6 +60,7 @@ static struct _opt {
{ "fencedcode", "fenced code blocks", 0, 0, 1, MKD_FENCEDCODE },
{ "idanchor", "id= anchors in TOC", 0, 0, 1, MKD_IDANCHOR },
{ "githubtags", "permit - and _ in element names", 0, 0, 0, MKD_GITHUBTAGS },
{ "urlencodedanchor", "urlencode special chars in TOC links", 0, 0, 0, MKD_URLENCODEDANCHOR },
} ;

#define NR(x) (sizeof x / sizeof x[0])
Expand Down
9 changes: 9 additions & 0 deletions tests/toc.t
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,14 @@ try '-T -ftoc' 'toc item with non-alpha start' \
<a name="L1.header"></a>
<h1>1 header</h1>'

# Be sure to save toc.t as UTF-8.
try '-T -ftoc,urlencodedanchor' 'urlencoded multibyte chars' \
'#It’s an apostrophe' \
'<ul>
<li><a href="#It%e2%80%99s%20an%20apostrophe">It’s an apostrophe</a></li>
</ul>
<a name="It%e2%80%99s%20an%20apostrophe"></a>
<h1>It’s an apostrophe</h1>'

summary $0
exit $rc
4 changes: 2 additions & 2 deletions toc.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ mkd_toc(Document *p, char **doc)
Csprintf(&res, "%*s<li><a href=\"#", srcp->hnumber, "");
mkd_string_to_anchor(T(srcp->text->text),
S(srcp->text->text),
(mkd_sta_function_t)Csputc, &res,1);
(mkd_sta_function_t)Csputc, &res,1,p->ctx->flags);
Csprintf(&res, "\">");
mkd_string_to_anchor(T(srcp->text->text),
S(srcp->text->text),
(mkd_sta_function_t)Csputc, &res,0);
(mkd_sta_function_t)Csputc, &res,0,p->ctx->flags);
Csprintf(&res, "</a>");

first = 0;
Expand Down

0 comments on commit 5da3388

Please sign in to comment.