Permalink
Browse files

Bring back changes from Rinku

  • Loading branch information...
1 parent c744346 commit 11d2add5b43e6c209ca4f7c4f326832f0ba63f70 @vmg committed Jul 8, 2012
Showing with 58 additions and 19 deletions.
  1. +41 −9 src/autolink.c
  2. +14 −7 src/autolink.h
  3. +3 −3 src/markdown.c
View
@@ -15,6 +15,7 @@
*/
#include "buffer.h"
+#include "autolink.h"
#include <string.h>
#include <stdlib.h>
@@ -132,7 +133,7 @@ autolink_delim(uint8_t *data, size_t link_end, size_t offset, size_t size)
}
static size_t
-check_domain(uint8_t *data, size_t size)
+check_domain(uint8_t *data, size_t size, int allow_short)
{
size_t i, np = 0;
@@ -144,13 +145,27 @@ check_domain(uint8_t *data, size_t size)
else if (!isalnum(data[i]) && data[i] != '-') break;
}
- /* a valid domain needs to have at least a dot.
- * that's as far as we get */
- return np ? i : 0;
+ if (allow_short) {
+ /* We don't need a valid domain in the strict sense (with
+ * least one dot; so just make sure it's composed of valid
+ * domain characters and return the length of the the valid
+ * sequence. */
+ return i;
+ } else {
+ /* a valid domain needs to have at least a dot.
+ * that's as far as we get */
+ return np ? i : 0;
+ }
}
size_t
-sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
+sd_autolink__www(
+ size_t *rewind_p,
+ struct buf *link,
+ uint8_t *data,
+ size_t offset,
+ size_t size,
+ unsigned int flags)
{
size_t link_end;
@@ -160,7 +175,7 @@ sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offse
if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0)
return 0;
- link_end = check_domain(data, size);
+ link_end = check_domain(data, size, 0);
if (link_end == 0)
return 0;
@@ -180,7 +195,13 @@ sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offse
}
size_t
-sd_autolink__email(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
+sd_autolink__email(
+ size_t *rewind_p,
+ struct buf *link,
+ uint8_t *data,
+ size_t offset,
+ size_t size,
+ unsigned int flags)
{
size_t link_end, rewind;
int nb = 0, np = 0;
@@ -229,7 +250,13 @@ sd_autolink__email(size_t *rewind_p, struct buf *link, uint8_t *data, size_t off
}
size_t
-sd_autolink__url(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
+sd_autolink__url(
+ size_t *rewind_p,
+ struct buf *link,
+ uint8_t *data,
+ size_t offset,
+ size_t size,
+ unsigned int flags)
{
size_t link_end, rewind = 0, domain_len;
@@ -241,9 +268,14 @@ sd_autolink__url(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offse
if (!sd_autolink_issafe(data - rewind, size + rewind))
return 0;
+
link_end = strlen("://");
- domain_len = check_domain(data + link_end, size - link_end);
+ domain_len = check_domain(
+ data + link_end,
+ size - link_end,
+ flags & SD_AUTOLINK_SHORT_DOMAINS);
+
if (domain_len == 0)
return 0;
View
@@ -23,17 +23,24 @@
extern "C" {
#endif
-extern int
+enum {
+ SD_AUTOLINK_SHORT_DOMAINS = (1 << 0),
+};
+
+int
sd_autolink_issafe(const uint8_t *link, size_t link_len);
-extern size_t
-sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
+size_t
+sd_autolink__www(size_t *rewind_p, struct buf *link,
+ uint8_t *data, size_t offset, size_t size, unsigned int flags);
-extern size_t
-sd_autolink__email(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
+size_t
+sd_autolink__email(size_t *rewind_p, struct buf *link,
+ uint8_t *data, size_t offset, size_t size, unsigned int flags);
-extern size_t
-sd_autolink__url(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
+size_t
+sd_autolink__url(size_t *rewind_p, struct buf *link,
+ uint8_t *data, size_t offset, size_t size, unsigned int flags);
#ifdef __cplusplus
}
View
@@ -771,7 +771,7 @@ char_autolink_www(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_
link = rndr_newbuf(rndr, BUFFER_SPAN);
- if ((link_len = sd_autolink__www(&rewind, link, data, offset, size)) > 0) {
+ if ((link_len = sd_autolink__www(&rewind, link, data, offset, size, 0)) > 0) {
link_url = rndr_newbuf(rndr, BUFFER_SPAN);
BUFPUTSL(link_url, "http://");
bufput(link_url, link->data, link->size);
@@ -803,7 +803,7 @@ char_autolink_email(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, siz
link = rndr_newbuf(rndr, BUFFER_SPAN);
- if ((link_len = sd_autolink__email(&rewind, link, data, offset, size)) > 0) {
+ if ((link_len = sd_autolink__email(&rewind, link, data, offset, size, 0)) > 0) {
ob->size -= rewind;
rndr->cb.autolink(ob, link, MKDA_EMAIL, rndr->opaque);
}
@@ -823,7 +823,7 @@ char_autolink_url(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_
link = rndr_newbuf(rndr, BUFFER_SPAN);
- if ((link_len = sd_autolink__url(&rewind, link, data, offset, size)) > 0) {
+ if ((link_len = sd_autolink__url(&rewind, link, data, offset, size, 0)) > 0) {
ob->size -= rewind;
rndr->cb.autolink(ob, link, MKDA_NORMAL, rndr->opaque);
}

0 comments on commit 11d2add

Please sign in to comment.