ext/rinku/rinku.c in rinku-1.5.1 vs ext/rinku/rinku.c in rinku-1.7.0
- old
+ new
@@ -16,10 +16,12 @@
#define RSTRING_NOT_MODIFIED
#include <stdio.h>
#include "ruby.h"
+#define RUBY_EXPORT __attribute__ ((visibility ("default")))
+
#ifdef HAVE_RUBY_ENCODING_H
#include <ruby/encoding.h>
#else
#define rb_enc_copy(dst, src)
#endif
@@ -41,15 +43,15 @@
} html_tag;
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,
AUTOLINK_ACTION_WWW,
AUTOLINK_ACTION_EMAIL,
@@ -183,10 +185,11 @@
int
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,
void (*link_text_cb)(struct buf *ob, const struct buf *link, void *payload),
void *payload)
@@ -202,14 +205,14 @@
memset(active_chars, 0x0, sizeof(active_chars));
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;
}
@@ -245,11 +248,11 @@
continue;
}
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) {
bufput(ob, text + i, end - i - rewind);
@@ -315,12 +318,12 @@
/*
* 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.
*
* NOTE: The block of text may or may not be HTML; if the text is HTML,
@@ -359,10 +362,13 @@
*
* - `skip_tags` is a list of strings with the names of HTML tags that will be skipped
* 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.
*
* ~~~~~ruby
@@ -375,18 +381,20 @@
static VALUE
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);
if (!NIL_P(rb_mode)) {
Check_Type(rb_mode, T_SYMBOL);
@@ -407,10 +415,15 @@
skip_tags = SKIP_TAGS;
} else {
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"))
link_mode = AUTOLINK_ALL;
else if (mode_sym == rb_intern("email_addresses"))
@@ -424,10 +437,11 @@
count = rinku_autolink(
output_buf,
RSTRING_PTR(rb_text),
RSTRING_LEN(rb_text),
link_mode,
+ link_flags,
link_attr,
skip_tags,
RTEST(rb_block) ? &autolink_callback : NULL,
(void*)rb_block);
@@ -443,11 +457,12 @@
bufrelease(output_buf);
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));
}