murdoc 0.1.6 → 0.1.7

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.7
@@ -47,25 +47,9 @@ module Murdoc
47
47
  def source=(src)
48
48
  @source = src
49
49
  @paragraphs = []
50
-
51
- # Lambda for checking source for comments. Used for getting consequent non-comments
52
- # into resulting stream
53
- is_comment = lambda do |line|
54
- result = false
55
- # If source supports single line comments
56
- if comment_symbols[:single_line]
57
- result ||= line =~ /^\s*#{Regexp.escape(comment_symbols[:single_line])}/
58
- end
59
-
60
- # If source supports multi-line comments
61
- if comment_symbols[:multiline]
62
- result ||= line =~ /^\s*#{Regexp.escape(comment_symbols[:multiline][:begin])}/
63
- end
64
- result
65
- end
50
+ lines = src.split("\n")
66
51
 
67
52
  # splitting stuff into lines and setting cursor into initial position
68
- lines = src.split("\n")
69
53
  i = 0
70
54
  while i < lines.size
71
55
  comment_lines = []
@@ -93,22 +77,19 @@ module Murdoc
93
77
  # getting source lines
94
78
  starting_line = i
95
79
  source_lines = []
96
- while i < lines.size && !is_comment.call(lines[i])
80
+ while i < lines.size && !is_comment(lines[i])
97
81
  source_lines << lines[i]
98
82
  i += 1
99
83
  end
84
+
100
85
  # post-processing: stripping comments and removing empty strings from beginnings and ends
101
- while source_lines.size > 0 && source_lines[0] =~ /^\s*$/
102
- starting_line += 1
103
- source_lines.delete_at(0)
86
+ starting_line += postprocess_source(source_lines)
87
+ postprocess_comments(comment_lines)
88
+
89
+ # writing a new paragraph unless we have a case of commented out code
90
+ unless comment_lines[0] =~ /^:code:$/
91
+ @paragraphs << Paragraph.new(source_lines.join("\n"), comment_lines.join("\n"), starting_line, source_type, options)
104
92
  end
105
- source_lines.delete_at(-1) while source_lines.size > 0 && source_lines[-1] =~ /^\s*$/
106
- comment_lines.map! {|l| l.sub(/^\s([^\s])/, '\\1').rstrip }
107
- comment_lines.delete_at(0) while comment_lines.size > 0 && comment_lines[0].empty?
108
- comment_lines.delete_at(-1) while comment_lines.size > 0 && comment_lines[-1].empty?
109
-
110
- # writing a new paragraph
111
- @paragraphs << Paragraph.new(source_lines.join("\n"), comment_lines.join("\n"), starting_line, source_type, options)
112
93
  end
113
94
  end
114
95
 
@@ -116,10 +97,52 @@ module Murdoc
116
97
  def source
117
98
  @source
118
99
  end
119
-
100
+
120
101
  protected
121
102
  def comment_symbols
122
103
  super || {}
123
104
  end
105
+
106
+ # Method for checking source for comments. Used for getting consequent non-comments
107
+ # into resulting stream
108
+ def is_comment(line)
109
+ result = false
110
+ # If source supports single line comments
111
+ if comment_symbols[:single_line]
112
+ result ||= line =~ /^\s*#{Regexp.escape(comment_symbols[:single_line])}/
113
+ end
114
+
115
+ # If source supports multi-line comments
116
+ if comment_symbols[:multiline]
117
+ result ||= line =~ /^\s*#{Regexp.escape(comment_symbols[:multiline][:begin])}/
118
+ end
119
+ result
120
+ end
121
+
122
+ #
123
+ # Removes trailing spaces from comments, also removes first space from comments,
124
+ # but only if there's only one space there (not to mess with markdown)
125
+ #
126
+ def postprocess_comments(comment_lines)
127
+ comment_lines.map! {|l| l.sub(/^\s([^\s])/, '\\1').rstrip }
128
+ comment_lines.delete_at(0) while comment_lines.size > 0 && comment_lines[0].empty?
129
+ comment_lines.delete_at(-1) while comment_lines.size > 0 && comment_lines[-1].empty?
130
+ end
131
+
132
+
133
+ # Removes blank lines from beginning and end of source lines
134
+ # returns starting offset for source (number of lines deleted from beginning)
135
+ def postprocess_source(source_lines)
136
+ starting_offset = 0
137
+ while source_lines.size > 0 && source_lines[0] =~ /^\s*$/
138
+ starting_offset += 1
139
+ source_lines.delete_at(0)
140
+ end
141
+
142
+ while source_lines.size > 0 && source_lines[-1] =~ /^\s*$/
143
+ source_lines.delete_at(-1)
144
+ end
145
+ starting_offset
146
+ end
124
147
  end
125
148
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{murdoc}
8
- s.version = "0.1.6"
8
+ s.version = "0.1.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mark Abramov"]
12
- s.date = %q{2011-01-25}
12
+ s.date = %q{2011-01-27}
13
13
  s.default_executable = %q{murdoc}
14
14
  s.description = %q{Annotated documentation generator, see README.md for details}
15
15
  s.email = %q{markizko@gmail.com}
@@ -78,6 +78,14 @@ describe Murdoc::Annotator do
78
78
  subject.paragraphs[1].source.should =~ /\A\s*def yo\s*puts 'rap'\s*end\s*\Z/m
79
79
  subject.paragraphs[1].annotation.should =~ /\ABlock two\Z/m
80
80
  end
81
+
82
+ it "should not hang upon non-closed comments" do
83
+ source = "=begin\n"
84
+ lambda {
85
+ subject = described_class.new(source, :ruby)
86
+ subject.source = source
87
+ }.should_not raise_error
88
+ end
81
89
  end
82
90
 
83
91
  context "for comment without code" do
@@ -88,6 +96,15 @@ describe Murdoc::Annotator do
88
96
  subject.paragraphs[0].annotation.should == "Header"
89
97
  end
90
98
  end
99
+
100
+ context "for commented code" do
101
+ let(:source) { "# :code:\n# def hello\n# end\n\n\# Comment\ndef hi\nend" }
102
+ it "should not create paragraphs" do
103
+ subject.should have_exactly(1).paragraphs
104
+ subject.paragraphs[0].source.should == "def hi\nend"
105
+ subject.paragraphs[0].annotation.should == "Comment"
106
+ end
107
+ end
91
108
 
92
109
  it "should not choke on edge cases" do
93
110
  subject.source = ""
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 6
9
- version: 0.1.6
8
+ - 7
9
+ version: 0.1.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mark Abramov
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-25 00:00:00 +03:00
17
+ date: 2011-01-27 00:00:00 +03:00
18
18
  default_executable: murdoc
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -151,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
151
  requirements:
152
152
  - - ">="
153
153
  - !ruby/object:Gem::Version
154
- hash: 2317771152433802766
154
+ hash: -231788783804928032
155
155
  segments:
156
156
  - 0
157
157
  version: "0"