tight-redcarpet 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/redcarpet ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # Usage: redcarpet [--parse-<extension>...] [--render-<extension>...] [--smarty] [<file>...]
3
+ # Convert one or more Markdown files to HTML and write to standard output. With
4
+ # no <file> or when <file> is '-', read Markdown source text from standard input.
5
+ # With <extension>s, perform additional Markdown processing before writing output.
6
+ # With --smarty, use the SmartyHTML renderer
7
+ if ARGV.include?('--help') or ARGV.include?('-h')
8
+ File.read(__FILE__).split("\n").grep(/^# /).each do |line|
9
+ puts line[2..-1]
10
+ end
11
+ exit 0
12
+ end
13
+
14
+ require 'redcarpet'
15
+
16
+ if ARGV.include?('--version') or ARGV.include?('-v')
17
+ puts "Redcarpet #{Redcarpet::VERSION}"
18
+ exit 0
19
+ end
20
+
21
+ root = File.expand_path('../../', __FILE__)
22
+ $:.unshift File.expand_path('lib', root)
23
+
24
+ render_extensions = {}
25
+ parse_extensions = {}
26
+ renderer = Redcarpet::Render::HTML
27
+
28
+ ARGV.delete_if do |arg|
29
+ if arg =~ /^--render-([\w-]+)$/
30
+ arg = $1.gsub('-', '_')
31
+ render_extensions[arg.to_sym] = true
32
+ elsif arg =~ /^--parse-([\w-]+)$/
33
+ arg = $1.gsub('-', '_')
34
+ parse_extensions[arg.to_sym] = true
35
+ elsif arg == '--smarty'
36
+ renderer = Redcarpet::Render::SmartyHTML
37
+ else
38
+ false
39
+ end
40
+ end
41
+
42
+ render = renderer.new(render_extensions)
43
+ STDOUT.write(Redcarpet::Markdown.new(render, parse_extensions).render(ARGF.read))
@@ -0,0 +1,296 @@
1
+ /*
2
+ * Copyright (c) 2011, Vicent Marti
3
+ *
4
+ * Permission to use, copy, modify, and distribute this software for any
5
+ * purpose with or without fee is hereby granted, provided that the above
6
+ * copyright notice and this permission notice appear in all copies.
7
+ *
8
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+ */
16
+
17
+ #include "buffer.h"
18
+ #include "autolink.h"
19
+
20
+ #include <string.h>
21
+ #include <stdlib.h>
22
+ #include <stdio.h>
23
+ #include <ctype.h>
24
+
25
+ #if defined(_WIN32)
26
+ #define strncasecmp _strnicmp
27
+ #endif
28
+
29
+ int
30
+ sd_autolink_issafe(const uint8_t *link, size_t link_len)
31
+ {
32
+ static const size_t valid_uris_count = 6;
33
+ static const char *valid_uris[] = {
34
+ "#", "/", "http://", "https://", "ftp://", "mailto:"
35
+ };
36
+
37
+ size_t i;
38
+
39
+ for (i = 0; i < valid_uris_count; ++i) {
40
+ size_t len = strlen(valid_uris[i]);
41
+
42
+ if (link_len > len &&
43
+ strncasecmp((char *)link, valid_uris[i], len) == 0 &&
44
+ isalnum(link[len]))
45
+ return 1;
46
+ }
47
+
48
+ return 0;
49
+ }
50
+
51
+ static size_t
52
+ autolink_delim(uint8_t *data, size_t link_end, size_t max_rewind, size_t size)
53
+ {
54
+ uint8_t cclose, copen = 0;
55
+ size_t i;
56
+
57
+ for (i = 0; i < link_end; ++i)
58
+ if (data[i] == '<') {
59
+ link_end = i;
60
+ break;
61
+ }
62
+
63
+ while (link_end > 0) {
64
+ if (strchr("?!.,", data[link_end - 1]) != NULL)
65
+ link_end--;
66
+
67
+ else if (data[link_end - 1] == ';') {
68
+ size_t new_end = link_end - 2;
69
+
70
+ while (new_end > 0 && isalpha(data[new_end]))
71
+ new_end--;
72
+
73
+ if (new_end < link_end - 2 && data[new_end] == '&')
74
+ link_end = new_end;
75
+ else
76
+ link_end--;
77
+ }
78
+ else break;
79
+ }
80
+
81
+ if (link_end == 0)
82
+ return 0;
83
+
84
+ cclose = data[link_end - 1];
85
+
86
+ switch (cclose) {
87
+ case '"': copen = '"'; break;
88
+ case '\'': copen = '\''; break;
89
+ case ')': copen = '('; break;
90
+ case ']': copen = '['; break;
91
+ case '}': copen = '{'; break;
92
+ }
93
+
94
+ if (copen != 0) {
95
+ size_t closing = 0;
96
+ size_t opening = 0;
97
+ size_t i = 0;
98
+
99
+ /* Try to close the final punctuation sign in this same line;
100
+ * if we managed to close it outside of the URL, that means that it's
101
+ * not part of the URL. If it closes inside the URL, that means it
102
+ * is part of the URL.
103
+ *
104
+ * Examples:
105
+ *
106
+ * foo http://www.pokemon.com/Pikachu_(Electric) bar
107
+ * => http://www.pokemon.com/Pikachu_(Electric)
108
+ *
109
+ * foo (http://www.pokemon.com/Pikachu_(Electric)) bar
110
+ * => http://www.pokemon.com/Pikachu_(Electric)
111
+ *
112
+ * foo http://www.pokemon.com/Pikachu_(Electric)) bar
113
+ * => http://www.pokemon.com/Pikachu_(Electric))
114
+ *
115
+ * (foo http://www.pokemon.com/Pikachu_(Electric)) bar
116
+ * => foo http://www.pokemon.com/Pikachu_(Electric)
117
+ */
118
+
119
+ while (i < link_end) {
120
+ if (data[i] == copen)
121
+ opening++;
122
+ else if (data[i] == cclose)
123
+ closing++;
124
+
125
+ i++;
126
+ }
127
+
128
+ if (closing != opening)
129
+ link_end--;
130
+ }
131
+
132
+ return link_end;
133
+ }
134
+
135
+ static size_t
136
+ check_domain(uint8_t *data, size_t size, int allow_short)
137
+ {
138
+ size_t i, np = 0;
139
+
140
+ if (!isalnum(data[0]))
141
+ return 0;
142
+
143
+ for (i = 1; i < size - 1; ++i) {
144
+ if (strchr(".:", data[i]) != NULL) np++;
145
+ else if (!isalnum(data[i]) && data[i] != '-') break;
146
+ }
147
+
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
+ }
159
+ }
160
+
161
+ size_t
162
+ sd_autolink__www(
163
+ size_t *rewind_p,
164
+ struct buf *link,
165
+ uint8_t *data,
166
+ size_t max_rewind,
167
+ size_t size,
168
+ unsigned int flags)
169
+ {
170
+ size_t link_end;
171
+
172
+ if (max_rewind > 0 && !ispunct(data[-1]) && !isspace(data[-1]))
173
+ return 0;
174
+
175
+ if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0)
176
+ return 0;
177
+
178
+ link_end = check_domain(data, size, 0);
179
+
180
+ if (link_end == 0)
181
+ return 0;
182
+
183
+ while (link_end < size && !isspace(data[link_end]))
184
+ link_end++;
185
+
186
+ link_end = autolink_delim(data, link_end, max_rewind, size);
187
+
188
+ if (link_end == 0)
189
+ return 0;
190
+
191
+ bufput(link, data, link_end);
192
+ *rewind_p = 0;
193
+
194
+ return (int)link_end;
195
+ }
196
+
197
+ size_t
198
+ sd_autolink__email(
199
+ size_t *rewind_p,
200
+ struct buf *link,
201
+ uint8_t *data,
202
+ size_t max_rewind,
203
+ size_t size,
204
+ unsigned int flags)
205
+ {
206
+ size_t link_end, rewind;
207
+ int nb = 0, np = 0;
208
+
209
+ for (rewind = 0; rewind < max_rewind; ++rewind) {
210
+ uint8_t c = data[-rewind - 1];
211
+
212
+ if (isalnum(c))
213
+ continue;
214
+
215
+ if (strchr(".+-_", c) != NULL)
216
+ continue;
217
+
218
+ break;
219
+ }
220
+
221
+ if (rewind == 0)
222
+ return 0;
223
+
224
+ for (link_end = 0; link_end < size; ++link_end) {
225
+ uint8_t c = data[link_end];
226
+
227
+ if (isalnum(c))
228
+ continue;
229
+
230
+ if (c == '@')
231
+ nb++;
232
+ else if (c == '.' && link_end < size - 1)
233
+ np++;
234
+ else if (c != '-' && c != '_')
235
+ break;
236
+ }
237
+
238
+ if (link_end < 2 || nb != 1 || np == 0)
239
+ return 0;
240
+
241
+ link_end = autolink_delim(data, link_end, max_rewind, size);
242
+
243
+ if (link_end == 0)
244
+ return 0;
245
+
246
+ bufput(link, data - rewind, link_end + rewind);
247
+ *rewind_p = rewind;
248
+
249
+ return link_end;
250
+ }
251
+
252
+ size_t
253
+ sd_autolink__url(
254
+ size_t *rewind_p,
255
+ struct buf *link,
256
+ uint8_t *data,
257
+ size_t max_rewind,
258
+ size_t size,
259
+ unsigned int flags)
260
+ {
261
+ size_t link_end, rewind = 0, domain_len;
262
+
263
+ if (size < 4 || data[1] != '/' || data[2] != '/')
264
+ return 0;
265
+
266
+ while (rewind < max_rewind && isalpha(data[-rewind - 1]))
267
+ rewind++;
268
+
269
+ if (!sd_autolink_issafe(data - rewind, size + rewind))
270
+ return 0;
271
+
272
+ link_end = strlen("://");
273
+
274
+ domain_len = check_domain(
275
+ data + link_end,
276
+ size - link_end,
277
+ flags & SD_AUTOLINK_SHORT_DOMAINS);
278
+
279
+ if (domain_len == 0)
280
+ return 0;
281
+
282
+ link_end += domain_len;
283
+ while (link_end < size && !isspace(data[link_end]))
284
+ link_end++;
285
+
286
+ link_end = autolink_delim(data, link_end, max_rewind, size);
287
+
288
+ if (link_end == 0)
289
+ return 0;
290
+
291
+ bufput(link, data - rewind, link_end + rewind);
292
+ *rewind_p = rewind;
293
+
294
+ return link_end;
295
+ }
296
+
@@ -0,0 +1,49 @@
1
+ /*
2
+ * Copyright (c) 2011, Vicent Marti
3
+ *
4
+ * Permission to use, copy, modify, and distribute this software for any
5
+ * purpose with or without fee is hereby granted, provided that the above
6
+ * copyright notice and this permission notice appear in all copies.
7
+ *
8
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+ */
16
+
17
+ #ifndef AUTOLINK_H__
18
+ #define AUTOLINK_H__
19
+
20
+ #include "buffer.h"
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ enum {
27
+ SD_AUTOLINK_SHORT_DOMAINS = (1 << 0),
28
+ };
29
+
30
+ int
31
+ sd_autolink_issafe(const uint8_t *link, size_t link_len);
32
+
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);
36
+
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);
40
+
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
48
+
49
+ #endif
@@ -0,0 +1,196 @@
1
+ /*
2
+ * Copyright (c) 2008, Natacha Porté
3
+ * Copyright (c) 2011, Vicent Martí
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+
18
+ #define BUFFER_MAX_ALLOC_SIZE (1024 * 1024 * 16) //16mb
19
+
20
+ #include "buffer.h"
21
+
22
+ #include <stdio.h>
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+ #include <assert.h>
26
+
27
+ /* MSVC compat */
28
+ #if defined(_MSC_VER)
29
+ # define _buf_vsnprintf _vsnprintf
30
+ #else
31
+ # define _buf_vsnprintf vsnprintf
32
+ #endif
33
+
34
+ int
35
+ bufprefix(const struct buf *buf, const char *prefix)
36
+ {
37
+ size_t i;
38
+ assert(buf && buf->unit);
39
+
40
+ for (i = 0; i < buf->size; ++i) {
41
+ if (prefix[i] == 0)
42
+ return 0;
43
+
44
+ if (buf->data[i] != prefix[i])
45
+ return buf->data[i] - prefix[i];
46
+ }
47
+
48
+ return 0;
49
+ }
50
+
51
+ /* bufgrow: increasing the allocated size to the given value */
52
+ int
53
+ bufgrow(struct buf *buf, size_t neosz)
54
+ {
55
+ size_t neoasz;
56
+ void *neodata;
57
+
58
+ assert(buf && buf->unit);
59
+
60
+ if (neosz > BUFFER_MAX_ALLOC_SIZE)
61
+ return BUF_ENOMEM;
62
+
63
+ if (buf->asize >= neosz)
64
+ return BUF_OK;
65
+
66
+ neoasz = buf->asize + buf->unit;
67
+ while (neoasz < neosz)
68
+ neoasz += buf->unit;
69
+
70
+ neodata = realloc(buf->data, neoasz);
71
+ if (!neodata)
72
+ return BUF_ENOMEM;
73
+
74
+ buf->data = neodata;
75
+ buf->asize = neoasz;
76
+ return BUF_OK;
77
+ }
78
+
79
+
80
+ /* bufnew: allocation of a new buffer */
81
+ struct buf *
82
+ bufnew(size_t unit)
83
+ {
84
+ struct buf *ret;
85
+ ret = malloc(sizeof (struct buf));
86
+
87
+ if (ret) {
88
+ ret->data = 0;
89
+ ret->size = ret->asize = 0;
90
+ ret->unit = unit;
91
+ }
92
+ return ret;
93
+ }
94
+
95
+ /* bufnullterm: NULL-termination of the string array */
96
+ const char *
97
+ bufcstr(struct buf *buf)
98
+ {
99
+ assert(buf && buf->unit);
100
+
101
+ if (buf->size < buf->asize && buf->data[buf->size] == 0)
102
+ return (char *)buf->data;
103
+
104
+ if (buf->size + 1 <= buf->asize || bufgrow(buf, buf->size + 1) == 0) {
105
+ buf->data[buf->size] = 0;
106
+ return (char *)buf->data;
107
+ }
108
+
109
+ return NULL;
110
+ }
111
+
112
+ /* bufprintf: formatted printing to a buffer */
113
+ void
114
+ bufprintf(struct buf *buf, const char *fmt, ...)
115
+ {
116
+ va_list ap;
117
+ int n;
118
+
119
+ assert(buf && buf->unit);
120
+
121
+ if (buf->size >= buf->asize && bufgrow(buf, buf->size + 1) < 0)
122
+ return;
123
+
124
+ va_start(ap, fmt);
125
+ n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
126
+ va_end(ap);
127
+
128
+ if (n < 0) {
129
+ #ifdef _MSC_VER
130
+ va_start(ap, fmt);
131
+ n = _vscprintf(fmt, ap);
132
+ va_end(ap);
133
+ #else
134
+ return;
135
+ #endif
136
+ }
137
+
138
+ if ((size_t)n >= buf->asize - buf->size) {
139
+ if (bufgrow(buf, buf->size + n + 1) < 0)
140
+ return;
141
+
142
+ va_start(ap, fmt);
143
+ n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
144
+ va_end(ap);
145
+ }
146
+
147
+ if (n < 0)
148
+ return;
149
+
150
+ buf->size += n;
151
+ }
152
+
153
+ /* bufput: appends raw data to a buffer */
154
+ void
155
+ bufput(struct buf *buf, const void *data, size_t len)
156
+ {
157
+ assert(buf && buf->unit);
158
+
159
+ if (buf->size + len > buf->asize && bufgrow(buf, buf->size + len) < 0)
160
+ return;
161
+
162
+ memcpy(buf->data + buf->size, data, len);
163
+ buf->size += len;
164
+ }
165
+
166
+ /* bufputs: appends a NUL-terminated string to a buffer */
167
+ void
168
+ bufputs(struct buf *buf, const char *str)
169
+ {
170
+ bufput(buf, str, strlen(str));
171
+ }
172
+
173
+
174
+ /* bufputc: appends a single uint8_t to a buffer */
175
+ void
176
+ bufputc(struct buf *buf, int c)
177
+ {
178
+ assert(buf && buf->unit);
179
+
180
+ if (buf->size + 1 > buf->asize && bufgrow(buf, buf->size + 1) < 0)
181
+ return;
182
+
183
+ buf->data[buf->size] = c;
184
+ buf->size += 1;
185
+ }
186
+
187
+ /* bufrelease: decrease the reference count and free the buffer if needed */
188
+ void
189
+ bufrelease(struct buf *buf)
190
+ {
191
+ if (!buf)
192
+ return;
193
+
194
+ free(buf->data);
195
+ free(buf);
196
+ }