rinku 1.5.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,12 +15,17 @@
15
15
  */
16
16
 
17
17
  #include "buffer.h"
18
+ #include "autolink.h"
18
19
 
19
20
  #include <string.h>
20
21
  #include <stdlib.h>
21
22
  #include <stdio.h>
22
23
  #include <ctype.h>
23
24
 
25
+ #if defined(_WIN32)
26
+ #define strncasecmp _strnicmp
27
+ #endif
28
+
24
29
  int
25
30
  sd_autolink_issafe(const uint8_t *link, size_t link_len)
26
31
  {
@@ -128,7 +133,7 @@ autolink_delim(uint8_t *data, size_t link_end, size_t offset, size_t size)
128
133
  }
129
134
 
130
135
  static size_t
131
- check_domain(uint8_t *data, size_t size)
136
+ check_domain(uint8_t *data, size_t size, int allow_short)
132
137
  {
133
138
  size_t i, np = 0;
134
139
 
@@ -140,13 +145,27 @@ check_domain(uint8_t *data, size_t size)
140
145
  else if (!isalnum(data[i]) && data[i] != '-') break;
141
146
  }
142
147
 
143
- /* a valid domain needs to have at least a dot.
144
- * that's as far as we get */
145
- return np ? i : 0;
148
+ if (allow_short) {
149
+ /* We don't need a valid domain in the strict sense (with
150
+ * least one dot; so just make sure it's composed of valid
151
+ * domain characters and return the length of the the valid
152
+ * sequence. */
153
+ return i;
154
+ } else {
155
+ /* a valid domain needs to have at least a dot.
156
+ * that's as far as we get */
157
+ return np ? i : 0;
158
+ }
146
159
  }
147
160
 
148
161
  size_t
149
- sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
162
+ sd_autolink__www(
163
+ size_t *rewind_p,
164
+ struct buf *link,
165
+ uint8_t *data,
166
+ size_t offset,
167
+ size_t size,
168
+ unsigned int flags)
150
169
  {
151
170
  size_t link_end;
152
171
 
@@ -156,7 +175,7 @@ sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offse
156
175
  if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0)
157
176
  return 0;
158
177
 
159
- link_end = check_domain(data, size);
178
+ link_end = check_domain(data, size, 0);
160
179
 
161
180
  if (link_end == 0)
162
181
  return 0;
@@ -176,7 +195,13 @@ sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offse
176
195
  }
177
196
 
178
197
  size_t
179
- sd_autolink__email(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
198
+ sd_autolink__email(
199
+ size_t *rewind_p,
200
+ struct buf *link,
201
+ uint8_t *data,
202
+ size_t offset,
203
+ size_t size,
204
+ unsigned int flags)
180
205
  {
181
206
  size_t link_end, rewind;
182
207
  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
225
250
  }
226
251
 
227
252
  size_t
228
- sd_autolink__url(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size)
253
+ sd_autolink__url(
254
+ size_t *rewind_p,
255
+ struct buf *link,
256
+ uint8_t *data,
257
+ size_t offset,
258
+ size_t size,
259
+ unsigned int flags)
229
260
  {
230
261
  size_t link_end, rewind = 0, domain_len;
231
262
 
@@ -237,9 +268,14 @@ sd_autolink__url(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offse
237
268
 
238
269
  if (!sd_autolink_issafe(data - rewind, size + rewind))
239
270
  return 0;
271
+
240
272
  link_end = strlen("://");
241
273
 
242
- domain_len = check_domain(data + link_end, size - link_end);
274
+ domain_len = check_domain(
275
+ data + link_end,
276
+ size - link_end,
277
+ flags & SD_AUTOLINK_SHORT_DOMAINS);
278
+
243
279
  if (domain_len == 0)
244
280
  return 0;
245
281
 
@@ -19,17 +19,32 @@
19
19
 
20
20
  #include "buffer.h"
21
21
 
22
- extern int
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ enum {
27
+ SD_AUTOLINK_SHORT_DOMAINS = (1 << 0),
28
+ };
29
+
30
+ int
23
31
  sd_autolink_issafe(const uint8_t *link, size_t link_len);
24
32
 
25
- extern size_t
26
- sd_autolink__www(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
33
+ size_t
34
+ sd_autolink__www(size_t *rewind_p, struct buf *link,
35
+ uint8_t *data, size_t offset, size_t size, unsigned int flags);
27
36
 
28
- extern size_t
29
- sd_autolink__email(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
37
+ size_t
38
+ sd_autolink__email(size_t *rewind_p, struct buf *link,
39
+ uint8_t *data, size_t offset, size_t size, unsigned int flags);
30
40
 
31
- extern size_t
32
- sd_autolink__url(size_t *rewind_p, struct buf *link, uint8_t *data, size_t offset, size_t size);
41
+ size_t
42
+ sd_autolink__url(size_t *rewind_p, struct buf *link,
43
+ uint8_t *data, size_t offset, size_t size, unsigned int flags);
44
+
45
+ #ifdef __cplusplus
46
+ }
47
+ #endif
33
48
 
34
49
  #endif
35
50
 
@@ -22,6 +22,7 @@
22
22
  #include <stdio.h>
23
23
  #include <stdlib.h>
24
24
  #include <string.h>
25
+ #include <assert.h>
25
26
 
26
27
  /* MSVC compat */
27
28
  #if defined(_MSC_VER)
@@ -34,6 +35,7 @@ int
34
35
  bufprefix(const struct buf *buf, const char *prefix)
35
36
  {
36
37
  size_t i;
38
+ assert(buf && buf->unit);
37
39
 
38
40
  for (i = 0; i < buf->size; ++i) {
39
41
  if (prefix[i] == 0)
@@ -52,7 +54,10 @@ bufgrow(struct buf *buf, size_t neosz)
52
54
  {
53
55
  size_t neoasz;
54
56
  void *neodata;
55
- if (!buf || !buf->unit || neosz > BUFFER_MAX_ALLOC_SIZE)
57
+
58
+ assert(buf && buf->unit);
59
+
60
+ if (neosz > BUFFER_MAX_ALLOC_SIZE)
56
61
  return BUF_ENOMEM;
57
62
 
58
63
  if (buf->asize >= neosz)
@@ -91,8 +96,7 @@ bufnew(size_t unit)
91
96
  const char *
92
97
  bufcstr(struct buf *buf)
93
98
  {
94
- if (!buf || !buf->unit)
95
- return NULL;
99
+ assert(buf && buf->unit);
96
100
 
97
101
  if (buf->size < buf->asize && buf->data[buf->size] == 0)
98
102
  return (char *)buf->data;
@@ -110,12 +114,11 @@ void
110
114
  bufprintf(struct buf *buf, const char *fmt, ...)
111
115
  {
112
116
  va_list ap;
113
- if (!buf || !buf->unit)
114
- return;
115
-
116
117
  int n;
117
118
 
118
- if (buf == 0 || (buf->size >= buf->asize && bufgrow(buf, buf->size + 1)) < 0)
119
+ assert(buf && buf->unit);
120
+
121
+ if (buf->size >= buf->asize && bufgrow(buf, buf->size + 1) < 0)
119
122
  return;
120
123
 
121
124
  va_start(ap, fmt);
@@ -124,7 +127,9 @@ bufprintf(struct buf *buf, const char *fmt, ...)
124
127
 
125
128
  if (n < 0) {
126
129
  #ifdef _MSC_VER
130
+ va_start(ap, fmt);
127
131
  n = _vscprintf(fmt, ap);
132
+ va_end(ap);
128
133
  #else
129
134
  return;
130
135
  #endif
@@ -133,6 +138,7 @@ bufprintf(struct buf *buf, const char *fmt, ...)
133
138
  if ((size_t)n >= buf->asize - buf->size) {
134
139
  if (bufgrow(buf, buf->size + n + 1) < 0)
135
140
  return;
141
+
136
142
  va_start(ap, fmt);
137
143
  n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
138
144
  va_end(ap);
@@ -148,8 +154,7 @@ bufprintf(struct buf *buf, const char *fmt, ...)
148
154
  void
149
155
  bufput(struct buf *buf, const void *data, size_t len)
150
156
  {
151
- if (!buf)
152
- return;
157
+ assert(buf && buf->unit);
153
158
 
154
159
  if (buf->size + len > buf->asize && bufgrow(buf, buf->size + len) < 0)
155
160
  return;
@@ -170,8 +175,7 @@ bufputs(struct buf *buf, const char *str)
170
175
  void
171
176
  bufputc(struct buf *buf, int c)
172
177
  {
173
- if (!buf)
174
- return;
178
+ assert(buf && buf->unit);
175
179
 
176
180
  if (buf->size + 1 > buf->asize && bufgrow(buf, buf->size + 1) < 0)
177
181
  return;
@@ -208,8 +212,7 @@ bufreset(struct buf *buf)
208
212
  void
209
213
  bufslurp(struct buf *buf, size_t len)
210
214
  {
211
- if (!buf || !buf->unit || len <= 0)
212
- return;
215
+ assert(buf && buf->unit);
213
216
 
214
217
  if (len >= buf->size) {
215
218
  buf->size = 0;
@@ -220,10 +223,3 @@ bufslurp(struct buf *buf, size_t len)
220
223
  memmove(buf->data, buf->data + len, buf->size);
221
224
  }
222
225
 
223
- /* vbufprintf: stdarg variant of formatted printing into a buffer */
224
- void
225
- vbufprintf(struct buf *buf, const char *fmt, va_list ap)
226
- {
227
-
228
- }
229
-
@@ -22,6 +22,10 @@
22
22
  #include <stdarg.h>
23
23
  #include <stdint.h>
24
24
 
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
25
29
  #if defined(_MSC_VER)
26
30
  #define __attribute__(x)
27
31
  #define inline
@@ -85,4 +89,8 @@ void bufslurp(struct buf *, size_t);
85
89
  /* bufprintf: formatted printing to a buffer */
86
90
  void bufprintf(struct buf *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
87
91
 
92
+ #ifdef __cplusplus
93
+ }
94
+ #endif
95
+
88
96
  #endif
@@ -1,4 +1,6 @@
1
1
  require 'mkmf'
2
2
 
3
+ $CFLAGS += ' -fvisibility=hidden'
4
+
3
5
  dir_config('rinku')
4
6
  create_makefile('rinku')
@@ -18,6 +18,8 @@
18
18
  #include <stdio.h>
19
19
  #include "ruby.h"
20
20
 
21
+ #define RUBY_EXPORT __attribute__ ((visibility ("default")))
22
+
21
23
  #ifdef HAVE_RUBY_ENCODING_H
22
24
  #include <ruby/encoding.h>
23
25
  #else
@@ -43,11 +45,11 @@ typedef enum {
43
45
  typedef enum {
44
46
  AUTOLINK_URLS = (1 << 0),
45
47
  AUTOLINK_EMAILS = (1 << 1),
46
- AUTOLINK_IN_CODE = (1 << 2),
47
48
  AUTOLINK_ALL = AUTOLINK_URLS|AUTOLINK_EMAILS
48
49
  } autolink_mode;
49
50
 
50
- typedef size_t (*autolink_parse_cb)(size_t *rewind, struct buf *, uint8_t *, size_t, size_t);
51
+ typedef size_t (*autolink_parse_cb)(
52
+ size_t *rewind, struct buf *, uint8_t *, size_t, size_t, unsigned int);
51
53
 
52
54
  typedef enum {
53
55
  AUTOLINK_ACTION_NONE = 0,
@@ -185,6 +187,7 @@ rinku_autolink(
185
187
  struct buf *ob,
186
188
  const uint8_t *text,
187
189
  size_t size,
190
+ autolink_mode mode,
188
191
  unsigned int flags,
189
192
  const char *link_attr,
190
193
  const char **skip_tags,
@@ -204,10 +207,10 @@ rinku_autolink(
204
207
 
205
208
  active_chars['<'] = AUTOLINK_ACTION_SKIP_TAG;
206
209
 
207
- if (flags & AUTOLINK_EMAILS)
210
+ if (mode & AUTOLINK_EMAILS)
208
211
  active_chars['@'] = AUTOLINK_ACTION_EMAIL;
209
212
 
210
- if (flags & AUTOLINK_URLS) {
213
+ if (mode & AUTOLINK_URLS) {
211
214
  active_chars['w'] = AUTOLINK_ACTION_WWW;
212
215
  active_chars['W'] = AUTOLINK_ACTION_WWW;
213
216
  active_chars[':'] = AUTOLINK_ACTION_URL;
@@ -247,7 +250,7 @@ rinku_autolink(
247
250
 
248
251
  link->size = 0;
249
252
  link_end = g_callbacks[(int)action](
250
- &rewind, link, (uint8_t *)text + end, end, size - end);
253
+ &rewind, link, (uint8_t *)text + end, end, size - end, flags);
251
254
 
252
255
  /* print the link */
253
256
  if (link_end > 0) {
@@ -317,8 +320,8 @@ const char **rinku_load_tags(VALUE rb_skip)
317
320
  * Document-method: auto_link
318
321
  *
319
322
  * call-seq:
320
- * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil)
321
- * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil) { |link_text| ... }
323
+ * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil, flags=0)
324
+ * auto_link(text, mode=:all, link_attr=nil, skip_tags=nil, flags=0) { |link_text| ... }
322
325
  *
323
326
  * Parses a block of text looking for "safe" urls or email addresses,
324
327
  * and turns them into HTML links with the given attributes.
@@ -361,6 +364,9 @@ const char **rinku_load_tags(VALUE rb_skip)
361
364
  * when autolinking. If `nil`, this defaults to the value of the global `Rinku.skip_tags`,
362
365
  * which is initially `["a", "pre", "code", "kbd", "script"]`.
363
366
  *
367
+ * - `flag` is an optional boolean value specifying whether to recognize
368
+ * 'http://foo' as a valid domain, or require at least one '.'. It defaults to false.
369
+ *
364
370
  * - `&block` is an optional block argument. If a block is passed, it will
365
371
  * be yielded for each found link in the text, and its return value will be used instead
366
372
  * of the name of the link. E.g.
@@ -377,14 +383,16 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
377
383
  {
378
384
  static const char *SKIP_TAGS[] = {"a", "pre", "code", "kbd", "script", NULL};
379
385
 
380
- VALUE result, rb_text, rb_mode, rb_html, rb_skip, rb_block;
386
+ VALUE result, rb_text, rb_mode, rb_html, rb_skip, rb_flags, rb_block;
381
387
  struct buf *output_buf;
382
388
  int link_mode, count;
389
+ unsigned int link_flags = 0;
383
390
  const char *link_attr = NULL;
384
391
  const char **skip_tags = NULL;
385
392
  ID mode_sym;
386
393
 
387
- rb_scan_args(argc, argv, "13&", &rb_text, &rb_mode, &rb_html, &rb_skip, &rb_block);
394
+ rb_scan_args(argc, argv, "14&", &rb_text, &rb_mode,
395
+ &rb_html, &rb_skip, &rb_flags, &rb_block);
388
396
 
389
397
  Check_Type(rb_text, T_STRING);
390
398
 
@@ -409,6 +417,11 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
409
417
  skip_tags = rinku_load_tags(rb_skip);
410
418
  }
411
419
 
420
+ if (!NIL_P(rb_flags)) {
421
+ Check_Type(rb_flags, T_FIXNUM);
422
+ link_flags = FIX2INT(rb_flags);
423
+ }
424
+
412
425
  output_buf = bufnew(32);
413
426
 
414
427
  if (mode_sym == rb_intern("all"))
@@ -426,6 +439,7 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
426
439
  RSTRING_PTR(rb_text),
427
440
  RSTRING_LEN(rb_text),
428
441
  link_mode,
442
+ link_flags,
429
443
  link_attr,
430
444
  skip_tags,
431
445
  RTEST(rb_block) ? &autolink_callback : NULL,
@@ -445,9 +459,10 @@ rb_rinku_autolink(int argc, VALUE *argv, VALUE self)
445
459
  return result;
446
460
  }
447
461
 
448
- void Init_rinku()
462
+ void RUBY_EXPORT Init_rinku()
449
463
  {
450
464
  rb_mRinku = rb_define_module("Rinku");
451
465
  rb_define_method(rb_mRinku, "auto_link", rb_rinku_autolink, -1);
466
+ rb_define_const(rb_mRinku, "AUTOLINK_SHORT_DOMAINS", INT2FIX(SD_AUTOLINK_SHORT_DOMAINS));
452
467
  }
453
468
 
@@ -0,0 +1,7 @@
1
+ #ifndef UPSKIRT_RINKU_H
2
+ #define UPSKIRT_RINKU_H
3
+
4
+ extern const VALUE
5
+ allow_short_domains(void);
6
+
7
+ #endif /* UPSKIRT_RINKU_H */
@@ -1,5 +1,5 @@
1
1
  module Rinku
2
- VERSION = "1.5.1"
2
+ VERSION = "1.7.0"
3
3
  attr_accessor :skip_tags
4
4
  extend self
5
5
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'rinku'
5
- s.version = '1.5.1'
5
+ s.version = '1.7.0'
6
6
  s.summary = "Mostly autolinking"
7
7
  s.description = <<-EOF
8
8
  A fast and very smart autolinking library that
@@ -10,13 +10,14 @@ Gem::Specification.new do |s|
10
10
  EOF
11
11
  s.email = 'vicent@github.com'
12
12
  s.homepage = 'http://github.com/tanoku/rinku'
13
- s.authors = ["Vicent Martí"]
13
+ s.authors = ["Vicent Marti"]
14
14
  # = MANIFEST =
15
15
  s.files = %w[
16
16
  COPYING
17
17
  README.markdown
18
18
  Rakefile
19
19
  ext/rinku/rinku.c
20
+ ext/rinku/rinku.h
20
21
  ext/rinku/autolink.c
21
22
  ext/rinku/autolink.h
22
23
  ext/rinku/buffer.c
@@ -154,6 +154,22 @@ This is just a test. <a href="http://www.pokemon.com">http://www.pokemon.com</a>
154
154
  assert_linked "<a href=\"#{url}\">#{url}</a>", url
155
155
  end
156
156
 
157
+ def test_autolink_options_for_short_domains
158
+ url = "http://google"
159
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
160
+ flags = Rinku::AUTOLINK_SHORT_DOMAINS
161
+
162
+ # Specifying use short_domains in the args
163
+ url = "http://google"
164
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
165
+ assert_equal Rinku.auto_link(url, nil, nil, nil, flags), linked_url
166
+
167
+ # Specifying no short_domains in the args
168
+ url = "http://google"
169
+ linked_url = "<a href=\"#{url}\">#{url}</a>"
170
+ assert_equal Rinku.auto_link(url, nil, nil, nil, 0), url
171
+ end
172
+
157
173
  def test_not_autolink_www
158
174
  assert_linked "Awww... man", "Awww... man"
159
175
  end
@@ -275,5 +291,5 @@ This is just a test. <a href="http://www.pokemon.com">http://www.pokemon.com</a>
275
291
  href ||= link_text
276
292
  %{<a href="#{CGI.escapeHTML href}">#{CGI.escapeHTML link_text}</a>}
277
293
  end
278
-
294
+
279
295
  end
metadata CHANGED
@@ -1,21 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rinku
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 5
9
- - 1
10
- version: 1.5.1
8
+ - 7
9
+ - 0
10
+ version: 1.7.0
11
11
  platform: ruby
12
12
  authors:
13
- - "Vicent Mart\xC3\xAD"
13
+ - Vicent Marti
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-13 00:00:00 Z
18
+ date: 2012-07-08 00:00:00 +02:00
19
+ default_executable:
19
20
  dependencies: []
20
21
 
21
22
  description: " A fast and very smart autolinking library that\n acts as a drop-in replacement for Rails `auto_link`\n"
@@ -31,6 +32,7 @@ files:
31
32
  - README.markdown
32
33
  - Rakefile
33
34
  - ext/rinku/rinku.c
35
+ - ext/rinku/rinku.h
34
36
  - ext/rinku/autolink.c
35
37
  - ext/rinku/autolink.h
36
38
  - ext/rinku/buffer.c
@@ -40,6 +42,7 @@ files:
40
42
  - lib/rails_rinku.rb
41
43
  - rinku.gemspec
42
44
  - test/autolink_test.rb
45
+ has_rdoc: true
43
46
  homepage: http://github.com/tanoku/rinku
44
47
  licenses: []
45
48
 
@@ -69,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
72
  requirements: []
70
73
 
71
74
  rubyforge_project:
72
- rubygems_version: 1.8.15
75
+ rubygems_version: 1.6.2
73
76
  signing_key:
74
77
  specification_version: 3
75
78
  summary: Mostly autolinking