wikitext 0.2 → 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/ary.h CHANGED
@@ -12,7 +12,7 @@
12
12
  // You should have received a copy of the GNU General Public License
13
13
  // along with this program. If not, see <http://www.gnu.org/licenses/>.
14
14
 
15
- #include <ruby/ruby.h>
15
+ #include <ruby.h>
16
16
 
17
17
  typedef struct
18
18
  {
@@ -192,20 +192,15 @@ inline void _Wikitext_indent(parser_t *parser)
192
192
  {
193
193
  char *old_end, *new_end;
194
194
  if (!parser->tabulation)
195
- {
196
195
  parser->tabulation = str_new_size(space_count);
197
- old_end = parser->tabulation->ptr;
198
- }
199
196
  else if (parser->tabulation->len < space_count)
200
- {
201
- old_end = parser->tabulation->ptr;
202
- str_grow(parser->tabulation, space_count);
203
- }
204
- else
205
- old_end = parser->tabulation->ptr;
197
+ str_grow(parser->tabulation, space_count); // reallocates if necessary
198
+ old_end = parser->tabulation->ptr + parser->tabulation->len;
206
199
  new_end = parser->tabulation->ptr + space_count;
207
200
  while (old_end < new_end)
208
201
  *old_end++ = ' ';
202
+ if (space_count > parser->tabulation->len)
203
+ parser->tabulation->len = space_count;
209
204
  rb_str_cat(parser->output, parser->tabulation->ptr, space_count);
210
205
  }
211
206
  parser->current_indent += 2;
@@ -823,16 +818,44 @@ inline void _Wikitext_rollback_failed_external_link(parser_t *parser)
823
818
  parser->link_text = Qnil;
824
819
  }
825
820
 
826
- VALUE Wikitext_parser_initialize(VALUE self)
821
+ VALUE Wikitext_parser_initialize(int argc, VALUE *argv, VALUE self)
827
822
  {
823
+ // process arguments
824
+ VALUE options;
825
+ if (rb_scan_args(argc, argv, "01", &options) == 0) // 0 mandatory arguments, 1 optional argument
826
+ options = Qnil;
827
+
828
+ // defaults
829
+ VALUE autolink = Qtrue;
830
+ VALUE line_ending = rb_str_new2("\n");
831
+ VALUE external_link_class = rb_str_new2("external");
832
+ VALUE mailto_class = rb_str_new2("mailto");
833
+ VALUE internal_link_prefix = rb_str_new2("/wiki/");
834
+ VALUE space_to_underscore = Qfalse;
835
+ VALUE treat_slash_as_special = Qtrue;
836
+
837
+ // process options hash (override defaults)
838
+ if (!NIL_P(options) && TYPE(options) == T_HASH)
839
+ {
840
+ #define OVERRIDE_IF_SET(name) \
841
+ NIL_P(rb_hash_aref(options, ID2SYM(rb_intern(#name)))) ? name : rb_hash_aref(options, ID2SYM(rb_intern(#name)))
842
+ autolink = OVERRIDE_IF_SET(autolink);
843
+ line_ending = OVERRIDE_IF_SET(line_ending);
844
+ external_link_class = OVERRIDE_IF_SET(external_link_class);
845
+ mailto_class = OVERRIDE_IF_SET(mailto_class);
846
+ internal_link_prefix = OVERRIDE_IF_SET(internal_link_prefix);
847
+ space_to_underscore = OVERRIDE_IF_SET(space_to_underscore);
848
+ treat_slash_as_special = OVERRIDE_IF_SET(treat_slash_as_special);
849
+ }
850
+
828
851
  // no need to call super here; rb_call_super()
829
- rb_iv_set(self, "@autolink", Qtrue);
830
- rb_iv_set(self, "@line_ending", rb_str_new2("\n"));
831
- rb_iv_set(self, "@external_link_class", rb_str_new2("external"));
832
- rb_iv_set(self, "@mailto_class", rb_str_new2("mailto"));
833
- rb_iv_set(self, "@internal_link_prefix", rb_str_new2("/wiki/"));
834
- rb_iv_set(self, "@space_to_underscore", Qfalse);
835
- rb_iv_set(self, "@treat_slash_as_special", Qtrue);
852
+ rb_iv_set(self, "@autolink", autolink);
853
+ rb_iv_set(self, "@line_ending", line_ending);
854
+ rb_iv_set(self, "@external_link_class", external_link_class);
855
+ rb_iv_set(self, "@mailto_class", mailto_class);
856
+ rb_iv_set(self, "@internal_link_prefix", internal_link_prefix);
857
+ rb_iv_set(self, "@space_to_underscore", space_to_underscore);
858
+ rb_iv_set(self, "@treat_slash_as_special", treat_slash_as_special);
836
859
  return self;
837
860
  }
838
861
 
@@ -12,9 +12,9 @@
12
12
  // You should have received a copy of the GNU General Public License
13
13
  // along with this program. If not, see <http://www.gnu.org/licenses/>.
14
14
 
15
- #include <ruby/ruby.h>
15
+ #include <ruby.h>
16
16
 
17
- VALUE Wikitext_parser_initialize(VALUE self);
17
+ VALUE Wikitext_parser_initialize(int argc, VALUE *argv, VALUE self);
18
18
 
19
19
  VALUE Wikitext_parser_tokenize(VALUE self, VALUE string);
20
20
 
data/ext/str.h CHANGED
@@ -12,7 +12,7 @@
12
12
  // You should have received a copy of the GNU General Public License
13
13
  // along with this program. If not, see <http://www.gnu.org/licenses/>.
14
14
 
15
- #include <ruby/ruby.h>
15
+ #include <ruby.h>
16
16
 
17
17
  typedef struct
18
18
  {
@@ -12,7 +12,7 @@
12
12
  // You should have received a copy of the GNU General Public License
13
13
  // along with this program. If not, see <http://www.gnu.org/licenses/>.
14
14
 
15
- #include <ruby/ruby.h>
15
+ #include <ruby.h>
16
16
  #include <stdint.h> /* uint32_t */
17
17
 
18
18
  #define TOKEN_TEXT(token) rb_str_new((const char *)token->start, (token->stop - token->start))
@@ -27,7 +27,7 @@ void Init_wikitext()
27
27
 
28
28
  // Wikitext::Parser
29
29
  cWikitextParser = rb_define_class_under(mWikitext, "Parser", rb_cObject);
30
- rb_define_method(cWikitextParser, "initialize", Wikitext_parser_initialize, 0);
30
+ rb_define_method(cWikitextParser, "initialize", Wikitext_parser_initialize, -1);
31
31
  rb_define_method(cWikitextParser, "parse", Wikitext_parser_parse, -1);
32
32
  rb_define_method(cWikitextParser, "profiling_parse", Wikitext_parser_profiling_parse, 1);
33
33
  rb_define_method(cWikitextParser, "tokenize", Wikitext_parser_tokenize, 1);
@@ -12,7 +12,7 @@
12
12
  // You should have received a copy of the GNU General Public License
13
13
  // along with this program. If not, see <http://www.gnu.org/licenses/>.
14
14
 
15
- #include <ruby/ruby.h>
15
+ #include <ruby.h>
16
16
  #include <stdint.h>
17
17
 
18
18
  #define ruby_inspect(obj) rb_funcall(rb_mKernel, rb_intern("p"), 1, obj)
@@ -21,9 +21,63 @@ describe Wikitext::Parser do
21
21
  @parser = Wikitext::Parser.new
22
22
  end
23
23
 
24
+ it 'should autolink by default' do
25
+ @parser.autolink.should == true
26
+ end
27
+
28
+ it 'should use linefeed as default line ending' do
29
+ @parser.line_ending.should == "\n"
30
+ end
31
+
32
+ it 'should use "external" as default external link class' do
33
+ @parser.external_link_class.should == 'external'
34
+ end
35
+
36
+ it 'should use "mailto" as default mailto class' do
37
+ @parser.mailto_class.should == 'mailto'
38
+ end
39
+
40
+ it 'should use "/wiki/" as default internal link prefix' do
41
+ @parser.internal_link_prefix.should == '/wiki/'
42
+ end
43
+
24
44
  it 'should turn space-to-underscore off by default' do
25
45
  @parser.space_to_underscore.should == false
26
46
  end
47
+
48
+ it 'should treat slash as special by default' do
49
+ @parser.treat_slash_as_special.should == true
50
+ end
51
+
52
+ describe 'overriding defaults at initialization time' do
53
+ it 'should allow overriding of autolink' do
54
+ Wikitext::Parser.new(:autolink => false).autolink.should == false
55
+ end
56
+
57
+ it 'should allow overriding of line ending' do
58
+ Wikitext::Parser.new(:line_ending => "\r").line_ending.should == "\r"
59
+ end
60
+
61
+ it 'should allow overriding of external link class' do
62
+ Wikitext::Parser.new(:external_link_class => 'foo').external_link_class.should == 'foo'
63
+ end
64
+
65
+ it 'should allow overriding of mailto class' do
66
+ Wikitext::Parser.new(:mailto_class => 'bar').mailto_class.should == 'bar'
67
+ end
68
+
69
+ it 'should allow overriding of internal link prefix' do
70
+ Wikitext::Parser.new(:internal_link_prefix => '/baz/').internal_link_prefix.should == '/baz/'
71
+ end
72
+
73
+ it 'should allow overriding of space-to-underscore' do
74
+ Wikitext::Parser.new(:space_to_underscore => true).space_to_underscore.should == true
75
+ end
76
+
77
+ it 'should allow overriding of treat slash as special' do
78
+ Wikitext::Parser.new(:treat_slash_as_special => false).treat_slash_as_special.should == false
79
+ end
80
+ end
27
81
  end
28
82
 
29
83
  describe Wikitext::Parser, 'parsing non-ASCII input' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikitext
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wincent Colaiuta
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-18 00:00:00 +01:00
12
+ date: 2008-02-19 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15