Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

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.
  • 6 commits
  • 12 files changed
  • 0 commit comments
  • 3 contributors
Commits on Jul 08, 2012
@vmg vmg Refactor the autolink flags passing 4df9e57
@vmg vmg Move the export flags to a define 6a91177
@vmg vmg Bump to 1.7.0 edc1f08
Commits on Aug 01, 2012
@vmg vmg Hey I'm @vmg a2e9834
Commits on Aug 13, 2012
@bquorning bquorning tanoku → vmg 49ab134
Commits on Aug 15, 2012
@vmg vmg Merge pull request #24 from bquorning/tanoku_vs_vmg
Update repository location in gemspec
d35027b
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
42 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,7 +145,7 @@ check_domain(uint8_t *data, size_t size)
else if (!isalnum(data[i]) && data[i] != '-') break;
}
- if (allow_short_domains()) {
+ 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
@@ -154,7 +159,13 @@ check_domain(uint8_t *data, size_t size)
}
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;
@@ -164,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;
@@ -184,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;
@@ -233,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;
@@ -245,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,19 +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);
-#include "rinku.h"
+#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
65 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,
@@ -71,21 +73,6 @@ static const char *g_hrefs[] = {
"<a href=\"",
};
-static VALUE short_domains = 0;
-
-const VALUE
-allow_short_domains(void)
-{
- return short_domains;
-}
-
-static VALUE
-set_allow_short_domains(VALUE val)
-{
- short_domains = val;
- return short_domains;
-}
-
static void
autolink__print(struct buf *ob, const struct buf *link, void *payload)
{
@@ -200,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,
@@ -219,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;
@@ -262,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) {
@@ -332,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, short_domains=false)
- * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil, short_domains=false) { |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.
@@ -376,7 +364,7 @@ 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"]`.
*
- * - `short_domains` is an optional boolean value specifying whether to recognize
+ * - `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
@@ -395,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_shortdomains, 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, "14&", &rb_text, &rb_mode, &rb_html, &rb_skip, &rb_shortdomains, &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);
@@ -427,23 +417,10 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
skip_tags = rinku_load_tags(rb_skip);
}
- if (NIL_P(rb_shortdomains))
- rb_shortdomains = rb_iv_get(self, "@short_domains");
-
- if (!NIL_P(rb_shortdomains)) {
- switch (TYPE(rb_shortdomains)) {
- case T_TRUE:
- short_domains = 1;
- break;
- case T_FALSE:
- short_domains = 0;
- break;
- default:
- /* raise exception */
- rb_raise(rb_eTypeError, "'short_domains' needs to be true or false!");
- break;
- }
- }
+ if (!NIL_P(rb_flags)) {
+ Check_Type(rb_flags, T_FIXNUM);
+ link_flags = FIX2INT(rb_flags);
+ }
output_buf = bufnew(32);
@@ -462,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,
@@ -481,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
4 lib/rinku.rb
@@ -1,6 +1,6 @@
module Rinku
- VERSION = "1.5.1"
- attr_accessor :skip_tags, :short_domains
+ VERSION = "1.7.0"
+ attr_accessor :skip_tags
extend self
end
View
6 rinku.gemspec
@@ -2,15 +2,15 @@
Gem::Specification.new do |s|
s.name = 'rinku'
- s.version = '1.5.1.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
2  sundown
@@ -1 +1 @@
-Subproject commit 63d995158ec8e5f988f34086f96376b93ac751f9
+Subproject commit 11d2add5b43e6c209ca4f7c4f326832f0ba63f70
View
17 test/autolink_test.rb
@@ -155,28 +155,19 @@ def test_autolink_works
end
def test_autolink_options_for_short_domains
- assert_equal Rinku.short_domains, nil
- Rinku.short_domains = true
- assert_equal Rinku.short_domains, true
-
url = "http://google"
linked_url = "<a href=\"#{url}\">#{url}</a>"
- assert_linked linked_url, url
-
- Rinku.short_domains = false
- assert_equal Rinku.short_domains, false
- url = "http://google"
- assert_equal Rinku.auto_link(url), url
+ 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, true), linked_url
+ 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, false), url
+ assert_equal Rinku.auto_link(url, nil, nil, nil, 0), url
end
def test_not_autolink_www
@@ -300,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.