Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 13 commits
  • 13 files changed
  • 0 commit comments
  • 4 contributors
Showing with 151 additions and 55 deletions.
  1. +1 −1 .gitmodules
  2. +2 −2 README.markdown
  3. +45 −9 ext/rinku/autolink.c
  4. +22 −7 ext/rinku/autolink.h
  5. +16 −20 ext/rinku/buffer.c
  6. +8 −0 ext/rinku/buffer.h
  7. +2 −0 ext/rinku/extconf.rb
  8. +25 −10 ext/rinku/rinku.c
  9. +7 −0 ext/rinku/rinku.h
  10. +1 −1 lib/rinku.rb
  11. +4 −3 rinku.gemspec
  12. +1 −1 sundown
  13. +17 −1 test/autolink_test.rb
View
2 .gitmodules
@@ -1,3 +1,3 @@
[submodule "sundown"]
path = sundown
- url = git://github.com/tanoku/sundown.git
+ url = git://github.com/vmg/sundown.git
View
4 README.markdown
@@ -16,7 +16,7 @@ Rinku is available as a Ruby gem:
The Rinku source is available at GitHub:
- $ git clone git://github.com/tanoku/rinku.git
+ $ git clone git://github.com/vmg/rinku.git
Rinku is a standalone library
-----------------------------
@@ -101,7 +101,7 @@ Rinku is written by me
I am Vicent Marti, and I wrote Rinku.
While Rinku is busy doing autolinks, you should be busy following me on twitter.
-[`@tanoku`](http://twitter.com/tanoku). Do it.
+[`@vmg`](http://twitter.com/vmg). Do it.
Rinku has an awesome license
----------------------------
View
54 ext/rinku/autolink.c
@@ -15,12 +15,17 @@
*/
#include "buffer.h"
+#include "autolink.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
+#if defined(_WIN32)
+#define strncasecmp _strnicmp
+#endif
+
int
sd_autolink_issafe(const uint8_t *link, size_t link_len)
{
@@ -128,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;
@@ -140,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;
@@ -156,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;
@@ -176,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;
@@ -225,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;
@@ -237,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
29 ext/rinku/autolink.h
@@ -19,17 +19,32 @@
#include "buffer.h"
-extern int
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+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
+}
+#endif
#endif
View
36 ext/rinku/buffer.c
@@ -22,6 +22,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
/* MSVC compat */
#if defined(_MSC_VER)
@@ -34,6 +35,7 @@ int
bufprefix(const struct buf *buf, const char *prefix)
{
size_t i;
+ assert(buf && buf->unit);
for (i = 0; i < buf->size; ++i) {
if (prefix[i] == 0)
@@ -52,7 +54,10 @@ bufgrow(struct buf *buf, size_t neosz)
{
size_t neoasz;
void *neodata;
- if (!buf || !buf->unit || neosz > BUFFER_MAX_ALLOC_SIZE)
+
+ assert(buf && buf->unit);
+
+ if (neosz > BUFFER_MAX_ALLOC_SIZE)
return BUF_ENOMEM;
if (buf->asize >= neosz)
@@ -91,8 +96,7 @@ bufnew(size_t unit)
const char *
bufcstr(struct buf *buf)
{
- if (!buf || !buf->unit)
- return NULL;
+ assert(buf && buf->unit);
if (buf->size < buf->asize && buf->data[buf->size] == 0)
return (char *)buf->data;
@@ -110,12 +114,11 @@ void
bufprintf(struct buf *buf, const char *fmt, ...)
{
va_list ap;
- if (!buf || !buf->unit)
- return;
-
int n;
- if (buf == 0 || (buf->size >= buf->asize && bufgrow(buf, buf->size + 1)) < 0)
+ assert(buf && buf->unit);
+
+ if (buf->size >= buf->asize && bufgrow(buf, buf->size + 1) < 0)
return;
va_start(ap, fmt);
@@ -124,7 +127,9 @@ bufprintf(struct buf *buf, const char *fmt, ...)
if (n < 0) {
#ifdef _MSC_VER
+ va_start(ap, fmt);
n = _vscprintf(fmt, ap);
+ va_end(ap);
#else
return;
#endif
@@ -133,6 +138,7 @@ bufprintf(struct buf *buf, const char *fmt, ...)
if ((size_t)n >= buf->asize - buf->size) {
if (bufgrow(buf, buf->size + n + 1) < 0)
return;
+
va_start(ap, fmt);
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
va_end(ap);
@@ -148,8 +154,7 @@ bufprintf(struct buf *buf, const char *fmt, ...)
void
bufput(struct buf *buf, const void *data, size_t len)
{
- if (!buf)
- return;
+ assert(buf && buf->unit);
if (buf->size + len > buf->asize && bufgrow(buf, buf->size + len) < 0)
return;
@@ -170,8 +175,7 @@ bufputs(struct buf *buf, const char *str)
void
bufputc(struct buf *buf, int c)
{
- if (!buf)
- return;
+ assert(buf && buf->unit);
if (buf->size + 1 > buf->asize && bufgrow(buf, buf->size + 1) < 0)
return;
@@ -208,8 +212,7 @@ bufreset(struct buf *buf)
void
bufslurp(struct buf *buf, size_t len)
{
- if (!buf || !buf->unit || len <= 0)
- return;
+ assert(buf && buf->unit);
if (len >= buf->size) {
buf->size = 0;
@@ -220,10 +223,3 @@ bufslurp(struct buf *buf, size_t len)
memmove(buf->data, buf->data + len, buf->size);
}
-/* vbufprintf: stdarg variant of formatted printing into a buffer */
-void
-vbufprintf(struct buf *buf, const char *fmt, va_list ap)
-{
-
-}
-
View
8 ext/rinku/buffer.h
@@ -22,6 +22,10 @@
#include <stdarg.h>
#include <stdint.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#if defined(_MSC_VER)
#define __attribute__(x)
#define inline
@@ -85,4 +89,8 @@ void bufslurp(struct buf *, size_t);
/* bufprintf: formatted printing to a buffer */
void bufprintf(struct buf *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
+#ifdef __cplusplus
+}
+#endif
+
#endif
View
2 ext/rinku/extconf.rb
@@ -1,4 +1,6 @@
require 'mkmf'
+$CFLAGS += ' -fvisibility=hidden'
+
dir_config('rinku')
create_makefile('rinku')
View
35 ext/rinku/rinku.c
@@ -18,6 +18,8 @@
#include <stdio.h>
#include "ruby.h"
+#define RUBY_EXPORT __attribute__ ((visibility ("default")))
+
#ifdef HAVE_RUBY_ENCODING_H
#include <ruby/encoding.h>
#else
@@ -43,11 +45,11 @@ typedef enum {
typedef enum {
AUTOLINK_URLS = (1 << 0),
AUTOLINK_EMAILS = (1 << 1),
- AUTOLINK_IN_CODE = (1 << 2),
AUTOLINK_ALL = AUTOLINK_URLS|AUTOLINK_EMAILS
} autolink_mode;
-typedef size_t (*autolink_parse_cb)(size_t *rewind, struct buf *, uint8_t *, size_t, size_t);
+typedef size_t (*autolink_parse_cb)(
+ size_t *rewind, struct buf *, uint8_t *, size_t, size_t, unsigned int);
typedef enum {
AUTOLINK_ACTION_NONE = 0,
@@ -185,6 +187,7 @@ rinku_autolink(
struct buf *ob,
const uint8_t *text,
size_t size,
+ autolink_mode mode,
unsigned int flags,
const char *link_attr,
const char **skip_tags,
@@ -204,10 +207,10 @@ rinku_autolink(
active_chars['<'] = AUTOLINK_ACTION_SKIP_TAG;
- if (flags & AUTOLINK_EMAILS)
+ if (mode & AUTOLINK_EMAILS)
active_chars['@'] = AUTOLINK_ACTION_EMAIL;
- if (flags & AUTOLINK_URLS) {
+ if (mode & AUTOLINK_URLS) {
active_chars['w'] = AUTOLINK_ACTION_WWW;
active_chars['W'] = AUTOLINK_ACTION_WWW;
active_chars[':'] = AUTOLINK_ACTION_URL;
@@ -247,7 +250,7 @@ rinku_autolink(
link->size = 0;
link_end = g_callbacks[(int)action](
- &rewind, link, (uint8_t *)text + end, end, size - end);
+ &rewind, link, (uint8_t *)text + end, end, size - end, flags);
/* print the link */
if (link_end > 0) {
@@ -317,8 +320,8 @@ const char **rinku_load_tags(VALUE rb_skip)
* Document-method: auto_link
*
* call-seq:
- * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil)
- * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil) { |link_text| ... }
+ * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil, flags=0)
+ * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil, flags=0) { |link_text| ... }
*
* Parses a block of text looking for "safe" urls or email addresses,
* and turns them into HTML links with the given attributes.
@@ -361,6 +364,9 @@ const char **rinku_load_tags(VALUE rb_skip)
* when autolinking. If `nil`, this defaults to the value of the global `Rinku.skip_tags`,
* which is initially `["a", "pre", "code", "kbd", "script"]`.
*
+ * - `flag` is an optional boolean value specifying whether to recognize
+ * 'http://foo' as a valid domain, or require at least one '.'. It defaults to false.
+ *
* - `&block` is an optional block argument. If a block is passed, it will
* be yielded for each found link in the text, and its return value will be used instead
* of the name of the link. E.g.
@@ -377,14 +383,16 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
{
static const char *SKIP_TAGS[] = {"a", "pre", "code", "kbd", "script", NULL};
- VALUE result, rb_text, rb_mode, rb_html, rb_skip, rb_block;
+ VALUE result, rb_text, rb_mode, rb_html, rb_skip, rb_flags, rb_block;
struct buf *output_buf;
int link_mode, count;
+ unsigned int link_flags = 0;
const char *link_attr = NULL;
const char **skip_tags = NULL;
ID mode_sym;
- rb_scan_args(argc, argv, "13&", &rb_text, &rb_mode, &rb_html, &rb_skip, &rb_block);
+ rb_scan_args(argc, argv, "14&", &rb_text, &rb_mode,
+ &rb_html, &rb_skip, &rb_flags, &rb_block);
Check_Type(rb_text, T_STRING);
@@ -409,6 +417,11 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
skip_tags = rinku_load_tags(rb_skip);
}
+ if (!NIL_P(rb_flags)) {
+ Check_Type(rb_flags, T_FIXNUM);
+ link_flags = FIX2INT(rb_flags);
+ }
+
output_buf = bufnew(32);
if (mode_sym == rb_intern("all"))
@@ -426,6 +439,7 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
RSTRING_PTR(rb_text),
RSTRING_LEN(rb_text),
link_mode,
+ link_flags,
link_attr,
skip_tags,
RTEST(rb_block) ? &autolink_callback : NULL,
@@ -445,9 +459,10 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
return result;
}
-void Init_rinku()
+void RUBY_EXPORT Init_rinku()
{
rb_mRinku = rb_define_module("Rinku");
rb_define_method(rb_mRinku, "auto_link", rb_rinku_autolink, -1);
+ rb_define_const(rb_mRinku, "AUTOLINK_SHORT_DOMAINS", INT2FIX(SD_AUTOLINK_SHORT_DOMAINS));
}
View
7 ext/rinku/rinku.h
@@ -0,0 +1,7 @@
+#ifndef UPSKIRT_RINKU_H
+#define UPSKIRT_RINKU_H
+
+extern const VALUE
+allow_short_domains(void);
+
+#endif /* UPSKIRT_RINKU_H */
View
2 lib/rinku.rb
@@ -1,5 +1,5 @@
module Rinku
- VERSION = "1.5.1"
+ VERSION = "1.7.0"
attr_accessor :skip_tags
extend self
end
View
7 rinku.gemspec
@@ -2,21 +2,22 @@
Gem::Specification.new do |s|
s.name = 'rinku'
- s.version = '1.5.1'
+ s.version = '1.7.0'
s.summary = "Mostly autolinking"
s.description = <<-EOF
A fast and very smart autolinking library that
acts as a drop-in replacement for Rails `auto_link`
EOF
s.email = 'vicent@github.com'
- s.homepage = 'http://github.com/tanoku/rinku'
- s.authors = ["Vicent Martí"]
+ s.homepage = 'http://github.com/vmg/rinku'
+ s.authors = ["Vicent Marti"]
# = MANIFEST =
s.files = %w[
COPYING
README.markdown
Rakefile
ext/rinku/rinku.c
+ ext/rinku/rinku.h
ext/rinku/autolink.c
ext/rinku/autolink.h
ext/rinku/buffer.c
2 sundown
@@ -1 +1 @@
-Subproject commit 63d995158ec8e5f988f34086f96376b93ac751f9
+Subproject commit 11d2add5b43e6c209ca4f7c4f326832f0ba63f70
View
18 test/autolink_test.rb
@@ -154,6 +154,22 @@ def test_autolink_works
assert_linked "<a href=\"#{url}\">#{url}</a>", url
end
+ def test_autolink_options_for_short_domains
+ url = "http://google"
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
+ flags = Rinku::AUTOLINK_SHORT_DOMAINS
+
+ # Specifying use short_domains in the args
+ url = "http://google"
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
+ assert_equal Rinku.auto_link(url, nil, nil, nil, flags), linked_url
+
+ # Specifying no short_domains in the args
+ url = "http://google"
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
+ assert_equal Rinku.auto_link(url, nil, nil, nil, 0), url
+ end
+
def test_not_autolink_www
assert_linked "Awww... man", "Awww... man"
end
@@ -275,5 +291,5 @@ def generate_result(link_text, href = nil)
href ||= link_text
%{<a href="#{CGI.escapeHTML href}">#{CGI.escapeHTML link_text}</a>}
end
-
+
end

No commit comments for this range

Something went wrong with that request. Please try again.