Skip to content
Browse files

Improved autolinking

  • Loading branch information...
1 parent a130456 commit 972f533fbddd7085dbc42b84af8ee10e77b581c2 @vmg committed Jun 19, 2011
Showing with 42 additions and 26 deletions.
  1. +38 −16 ext/redcarpet/autolink.c
  2. +0 −6 ext/redcarpet/autolink.h
  3. +1 −1 lib/redcarpet.rb
  4. +2 −2 redcarpet.gemspec
  5. +1 −1 upskirt
View
54 ext/redcarpet/autolink.c
@@ -47,6 +47,13 @@ static size_t
autolink_delim(char *data, size_t link_end, size_t offset, size_t size)
{
char cclose, copen = 0;
+ size_t i;
+
+ for (i = 0; i < link_end; ++i)
+ if (data[i] == '<') {
+ link_end = i;
+ break;
+ }
while (link_end > 0) {
if (strchr("?!.,", data[link_end - 1]) != NULL)
@@ -63,11 +70,6 @@ autolink_delim(char *data, size_t link_end, size_t offset, size_t size)
else
link_end--;
}
-
- else if (data[link_end - 1] == '>') {
- while (link_end > 0 && data[link_end] != '<')
- link_end--;
- }
else break;
}
@@ -125,29 +127,44 @@ autolink_delim(char *data, size_t link_end, size_t offset, size_t size)
return link_end;
}
+static size_t
+check_domain(char *data, size_t size)
+{
+ size_t i, np = 0;
+
+ if (!isalnum(data[0]))
+ return 0;
+
+ for (i = 1; i < size - 1; ++i) {
+ if (data[i] == '.') np++;
+ else if (!isalnum(data[i]) && data[i] != '-') break;
+ }
+
+ if (!isalnum(data[i - 1]) || np == 0)
+ return 0;
+
+ return i;
+}
+
size_t
ups_autolink__www(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size)
{
size_t link_end;
- int np = 0;
if (offset > 0 && !ispunct(data[-1]) && !isspace(data[-1]))
return 0;
if (size < 4 || memcmp(data, "www.", STRLEN("www.")) != 0)
return 0;
- link_end = 0;
- while (link_end < size && !isspace(data[link_end])) {
- if (data[link_end] == '.')
- np++;
-
- link_end++;
- }
+ link_end = check_domain(data, size);
- if (np < 2)
+ if (link_end == 0)
return 0;
+ while (link_end < size && !isspace(data[link_end]))
+ link_end++;
+
link_end = autolink_delim(data, link_end, offset, size);
if (link_end == 0)
@@ -211,7 +228,7 @@ ups_autolink__email(size_t *rewind_p, struct buf *link, char *data, size_t offse
size_t
ups_autolink__url(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size)
{
- size_t link_end, rewind = 0;
+ size_t link_end, rewind = 0, domain_len;
if (size < 4 || data[1] != '/' || data[2] != '/')
return 0;
@@ -221,8 +238,13 @@ ups_autolink__url(size_t *rewind_p, struct buf *link, char *data, size_t offset,
if (!is_safe_link(data - rewind, size + rewind))
return 0;
+ link_end = STRLEN("://");
+
+ domain_len = check_domain(data + link_end, size - link_end);
+ if (domain_len == 0)
+ return 0;
- link_end = 0;
+ link_end += domain_len;
while (link_end < size && !isspace(data[link_end]))
link_end++;
View
6 ext/redcarpet/autolink.h
@@ -19,12 +19,6 @@
#include "buffer.h"
-typedef enum {
- AUTOLINK_URLS = (1 << 0),
- AUTOLINK_EMAILS = (1 << 1),
- AUTOLINK_ALL = AUTOLINK_URLS|AUTOLINK_EMAILS
-} autolink_mode;
-
extern size_t
ups_autolink__www(size_t *rewind_p, struct buf *link, char *data, size_t offset, size_t size);
View
2 lib/redcarpet.rb
@@ -26,7 +26,7 @@
# end
#
class Redcarpet
- VERSION = '1.17.1'
+ VERSION = '1.17.2'
# Original Markdown formatted text.
attr_reader :text
View
4 redcarpet.gemspec
@@ -1,9 +1,9 @@
Gem::Specification.new do |s|
s.name = 'redcarpet'
- s.version = '1.17.1'
+ s.version = '1.17.2'
s.summary = "Ruby bindings for libupskirt"
s.description = 'A fast and safe Markdown to (X)HTML parser'
- s.date = '2011-06-09'
+ s.date = '2011-06-19'
s.email = 'vicent@github.com'
s.homepage = 'http://github.com/tanoku/redcarpet'
s.authors = ["Natacha Porté", "Vicent Martí"]
2 upskirt
@@ -1 +1 @@
-Subproject commit 19115feb28c0284e3d47f47ecf9458a4da813163
+Subproject commit 3b0a641094ccfe6d6c7040bfa17f0421e110ef82

0 comments on commit 972f533

Please sign in to comment.
Something went wrong with that request. Please try again.