rdoc 3.1 → 6.3.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rdoc might be problematic. Click here for more details.

Files changed (247) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.rdoc +220 -0
  3. data/CVE-2013-0256.rdoc +49 -0
  4. data/ExampleMarkdown.md +37 -0
  5. data/ExampleRDoc.rdoc +208 -0
  6. data/Gemfile +12 -0
  7. data/History.rdoc +1666 -0
  8. data/LEGAL.rdoc +50 -0
  9. data/LICENSE.rdoc +57 -0
  10. data/README.rdoc +129 -0
  11. data/RI.rdoc +57 -0
  12. data/Rakefile +84 -81
  13. data/TODO.rdoc +59 -0
  14. data/bin/console +7 -0
  15. data/bin/setup +6 -0
  16. data/{bin → exe}/rdoc +11 -2
  17. data/exe/ri +12 -0
  18. data/lib/rdoc/alias.rb +1 -2
  19. data/lib/rdoc/anon_class.rb +3 -2
  20. data/lib/rdoc/any_method.rb +234 -40
  21. data/lib/rdoc/attr.rb +79 -11
  22. data/lib/rdoc/class_module.rb +443 -71
  23. data/lib/rdoc/code_object.rb +216 -20
  24. data/lib/rdoc/code_objects.rb +4 -21
  25. data/lib/rdoc/comment.rb +250 -0
  26. data/lib/rdoc/constant.rb +110 -9
  27. data/lib/rdoc/context/section.rb +232 -0
  28. data/lib/rdoc/context.rb +392 -172
  29. data/lib/rdoc/cross_reference.rb +202 -0
  30. data/lib/rdoc/encoding.rb +83 -28
  31. data/lib/rdoc/erb_partial.rb +19 -0
  32. data/lib/rdoc/erbio.rb +8 -3
  33. data/lib/rdoc/extend.rb +10 -0
  34. data/lib/rdoc/generator/darkfish.rb +507 -84
  35. data/lib/rdoc/generator/json_index.rb +300 -0
  36. data/lib/rdoc/generator/markup.rb +27 -74
  37. data/lib/rdoc/generator/pot/message_extractor.rb +68 -0
  38. data/lib/rdoc/generator/pot/po.rb +84 -0
  39. data/lib/rdoc/generator/pot/po_entry.rb +141 -0
  40. data/lib/rdoc/generator/pot.rb +98 -0
  41. data/lib/rdoc/generator/ri.rb +8 -62
  42. data/lib/rdoc/generator/template/darkfish/_footer.rhtml +5 -0
  43. data/lib/rdoc/generator/template/darkfish/_head.rhtml +22 -0
  44. data/lib/rdoc/generator/template/darkfish/_sidebar_VCS_info.rhtml +19 -0
  45. data/lib/rdoc/generator/template/darkfish/_sidebar_classes.rhtml +9 -0
  46. data/lib/rdoc/generator/template/darkfish/_sidebar_extends.rhtml +15 -0
  47. data/lib/rdoc/generator/template/darkfish/_sidebar_in_files.rhtml +9 -0
  48. data/lib/rdoc/generator/template/darkfish/_sidebar_includes.rhtml +15 -0
  49. data/lib/rdoc/generator/template/darkfish/_sidebar_installed.rhtml +15 -0
  50. data/lib/rdoc/generator/template/darkfish/_sidebar_methods.rhtml +12 -0
  51. data/lib/rdoc/generator/template/darkfish/_sidebar_navigation.rhtml +11 -0
  52. data/lib/rdoc/generator/template/darkfish/_sidebar_pages.rhtml +12 -0
  53. data/lib/rdoc/generator/template/darkfish/_sidebar_parent.rhtml +11 -0
  54. data/lib/rdoc/generator/template/darkfish/_sidebar_search.rhtml +14 -0
  55. data/lib/rdoc/generator/template/darkfish/_sidebar_sections.rhtml +11 -0
  56. data/lib/rdoc/generator/template/darkfish/_sidebar_table_of_contents.rhtml +18 -0
  57. data/lib/rdoc/generator/template/darkfish/class.rhtml +172 -0
  58. data/lib/rdoc/generator/template/darkfish/css/fonts.css +167 -0
  59. data/lib/rdoc/generator/template/darkfish/css/rdoc.css +639 -0
  60. data/lib/rdoc/generator/template/darkfish/fonts/Lato-Light.ttf +0 -0
  61. data/lib/rdoc/generator/template/darkfish/fonts/Lato-LightItalic.ttf +0 -0
  62. data/lib/rdoc/generator/template/darkfish/fonts/Lato-Regular.ttf +0 -0
  63. data/lib/rdoc/generator/template/darkfish/fonts/Lato-RegularItalic.ttf +0 -0
  64. data/lib/rdoc/generator/template/darkfish/fonts/SourceCodePro-Bold.ttf +0 -0
  65. data/lib/rdoc/generator/template/darkfish/fonts/SourceCodePro-Regular.ttf +0 -0
  66. data/lib/rdoc/generator/template/darkfish/images/add.png +0 -0
  67. data/lib/rdoc/generator/template/darkfish/images/arrow_up.png +0 -0
  68. data/lib/rdoc/generator/template/darkfish/images/delete.png +0 -0
  69. data/lib/rdoc/generator/template/darkfish/images/tag_blue.png +0 -0
  70. data/lib/rdoc/generator/template/darkfish/images/transparent.png +0 -0
  71. data/lib/rdoc/generator/template/darkfish/index.rhtml +18 -60
  72. data/lib/rdoc/generator/template/darkfish/js/darkfish.js +51 -83
  73. data/lib/rdoc/generator/template/darkfish/js/search.js +110 -0
  74. data/lib/rdoc/generator/template/darkfish/page.rhtml +18 -0
  75. data/lib/rdoc/generator/template/darkfish/servlet_not_found.rhtml +18 -0
  76. data/lib/rdoc/generator/template/darkfish/servlet_root.rhtml +62 -0
  77. data/lib/rdoc/generator/template/darkfish/table_of_contents.rhtml +58 -0
  78. data/lib/rdoc/generator/template/json_index/.document +1 -0
  79. data/lib/rdoc/generator/template/json_index/js/navigation.js +105 -0
  80. data/lib/rdoc/generator/template/json_index/js/searcher.js +229 -0
  81. data/lib/rdoc/generator.rb +24 -13
  82. data/lib/rdoc/ghost_method.rb +1 -2
  83. data/lib/rdoc/i18n/locale.rb +102 -0
  84. data/lib/rdoc/i18n/text.rb +126 -0
  85. data/lib/rdoc/i18n.rb +10 -0
  86. data/lib/rdoc/include.rb +5 -95
  87. data/lib/rdoc/known_classes.rb +5 -2
  88. data/lib/rdoc/markdown/entities.rb +2132 -0
  89. data/lib/rdoc/markdown/literals.kpeg +23 -0
  90. data/lib/rdoc/markdown/literals.rb +416 -0
  91. data/lib/rdoc/markdown.kpeg +1237 -0
  92. data/lib/rdoc/markdown.rb +16684 -0
  93. data/lib/rdoc/markup/attr_changer.rb +23 -0
  94. data/lib/rdoc/markup/attr_span.rb +36 -0
  95. data/lib/rdoc/markup/attribute_manager.rb +135 -62
  96. data/lib/rdoc/markup/attributes.rb +71 -0
  97. data/lib/rdoc/markup/blank_line.rb +1 -0
  98. data/lib/rdoc/markup/block_quote.rb +15 -0
  99. data/lib/rdoc/markup/document.rb +96 -9
  100. data/lib/rdoc/markup/formatter.rb +138 -25
  101. data/lib/rdoc/markup/hard_break.rb +32 -0
  102. data/lib/rdoc/markup/heading.rb +61 -2
  103. data/lib/rdoc/markup/include.rb +43 -0
  104. data/lib/rdoc/markup/indented_paragraph.rb +48 -0
  105. data/lib/rdoc/markup/list.rb +25 -4
  106. data/lib/rdoc/markup/list_item.rb +18 -4
  107. data/lib/rdoc/markup/paragraph.rb +15 -0
  108. data/lib/rdoc/markup/parser.rb +180 -88
  109. data/lib/rdoc/markup/pre_process.rb +183 -38
  110. data/lib/rdoc/markup/raw.rb +6 -5
  111. data/lib/rdoc/markup/regexp_handling.rb +41 -0
  112. data/lib/rdoc/markup/rule.rb +1 -0
  113. data/lib/rdoc/markup/table.rb +47 -0
  114. data/lib/rdoc/markup/to_ansi.rb +17 -7
  115. data/lib/rdoc/markup/to_bs.rb +5 -8
  116. data/lib/rdoc/markup/to_html.rb +238 -137
  117. data/lib/rdoc/markup/to_html_crossref.rb +125 -152
  118. data/lib/rdoc/markup/to_html_snippet.rb +285 -0
  119. data/lib/rdoc/markup/to_joined_paragraph.rb +47 -0
  120. data/lib/rdoc/markup/to_label.rb +75 -0
  121. data/lib/rdoc/markup/to_markdown.rb +192 -0
  122. data/lib/rdoc/markup/to_rdoc.rb +85 -15
  123. data/lib/rdoc/markup/to_table_of_contents.rb +89 -0
  124. data/lib/rdoc/markup/to_test.rb +2 -4
  125. data/lib/rdoc/markup/to_tt_only.rb +121 -0
  126. data/lib/rdoc/markup/verbatim.rb +39 -0
  127. data/lib/rdoc/markup.rb +388 -110
  128. data/lib/rdoc/meta_method.rb +1 -2
  129. data/lib/rdoc/method_attr.rb +87 -21
  130. data/lib/rdoc/mixin.rb +121 -0
  131. data/lib/rdoc/normal_class.rb +39 -10
  132. data/lib/rdoc/normal_module.rb +22 -7
  133. data/lib/rdoc/options.rb +613 -73
  134. data/lib/rdoc/parser/c.rb +621 -287
  135. data/lib/rdoc/parser/changelog.rb +335 -0
  136. data/lib/rdoc/parser/markdown.rb +24 -0
  137. data/lib/rdoc/parser/rd.rb +23 -0
  138. data/lib/rdoc/parser/ripper_state_lex.rb +590 -0
  139. data/lib/rdoc/parser/ruby.rb +1368 -762
  140. data/lib/rdoc/parser/ruby_tools.rb +42 -35
  141. data/lib/rdoc/parser/simple.rb +23 -11
  142. data/lib/rdoc/parser/text.rb +12 -0
  143. data/lib/rdoc/parser.rb +162 -89
  144. data/lib/rdoc/rd/block_parser.rb +1056 -0
  145. data/lib/rdoc/rd/block_parser.ry +639 -0
  146. data/lib/rdoc/rd/inline.rb +72 -0
  147. data/lib/rdoc/rd/inline_parser.rb +1208 -0
  148. data/lib/rdoc/rd/inline_parser.ry +593 -0
  149. data/lib/rdoc/rd.rb +100 -0
  150. data/lib/rdoc/rdoc.rb +208 -115
  151. data/lib/rdoc/require.rb +1 -2
  152. data/lib/rdoc/ri/driver.rb +734 -239
  153. data/lib/rdoc/ri/formatter.rb +1 -0
  154. data/lib/rdoc/ri/paths.rb +91 -48
  155. data/lib/rdoc/ri/store.rb +3 -261
  156. data/lib/rdoc/ri/task.rb +71 -0
  157. data/lib/rdoc/ri.rb +5 -2
  158. data/lib/rdoc/rubygems_hook.rb +246 -0
  159. data/lib/rdoc/servlet.rb +451 -0
  160. data/lib/rdoc/single_class.rb +14 -2
  161. data/lib/rdoc/stats/normal.rb +19 -12
  162. data/lib/rdoc/stats/quiet.rb +1 -0
  163. data/lib/rdoc/stats/verbose.rb +1 -0
  164. data/lib/rdoc/stats.rb +262 -104
  165. data/lib/rdoc/store.rb +979 -0
  166. data/lib/rdoc/task.rb +84 -44
  167. data/lib/rdoc/text.rb +117 -72
  168. data/lib/rdoc/token_stream.rb +73 -4
  169. data/lib/rdoc/tom_doc.rb +263 -0
  170. data/lib/rdoc/top_level.rb +111 -261
  171. data/lib/rdoc/version.rb +8 -0
  172. data/lib/rdoc.rb +127 -64
  173. data/man/ri.1 +247 -0
  174. data/rdoc.gemspec +249 -0
  175. metadata +171 -291
  176. data/.autotest +0 -16
  177. data/.document +0 -5
  178. data/History.txt +0 -594
  179. data/LICENSE.txt +0 -57
  180. data/Manifest.txt +0 -158
  181. data/README.txt +0 -45
  182. data/RI.txt +0 -58
  183. data/bin/ri +0 -5
  184. data/lib/rdoc/gauntlet.rb +0 -52
  185. data/lib/rdoc/generator/template/darkfish/classpage.rhtml +0 -296
  186. data/lib/rdoc/generator/template/darkfish/filepage.rhtml +0 -124
  187. data/lib/rdoc/generator/template/darkfish/js/jquery.js +0 -32
  188. data/lib/rdoc/generator/template/darkfish/js/quicksearch.js +0 -114
  189. data/lib/rdoc/generator/template/darkfish/js/thickbox-compressed.js +0 -10
  190. data/lib/rdoc/generator/template/darkfish/rdoc.css +0 -706
  191. data/lib/rdoc/markup/formatter_test_case.rb +0 -689
  192. data/lib/rdoc/markup/inline.rb +0 -137
  193. data/lib/rdoc/markup/text_formatter_test_case.rb +0 -116
  194. data/lib/rdoc/ruby_lex.rb +0 -1291
  195. data/lib/rdoc/ruby_token.rb +0 -416
  196. data/test/README +0 -1
  197. data/test/binary.dat +0 -0
  198. data/test/hidden.zip.txt +0 -1
  199. data/test/test.ja.rdoc +0 -10
  200. data/test/test.ja.txt +0 -8
  201. data/test/test.txt +0 -1
  202. data/test/test_attribute_manager.rb +0 -120
  203. data/test/test_rdoc_alias.rb +0 -13
  204. data/test/test_rdoc_any_method.rb +0 -126
  205. data/test/test_rdoc_attr.rb +0 -61
  206. data/test/test_rdoc_class_module.rb +0 -233
  207. data/test/test_rdoc_code_object.rb +0 -165
  208. data/test/test_rdoc_constant.rb +0 -15
  209. data/test/test_rdoc_context.rb +0 -370
  210. data/test/test_rdoc_encoding.rb +0 -166
  211. data/test/test_rdoc_generator_darkfish.rb +0 -119
  212. data/test/test_rdoc_generator_ri.rb +0 -76
  213. data/test/test_rdoc_include.rb +0 -96
  214. data/test/test_rdoc_markup.rb +0 -37
  215. data/test/test_rdoc_markup_attribute_manager.rb +0 -240
  216. data/test/test_rdoc_markup_document.rb +0 -51
  217. data/test/test_rdoc_markup_paragraph.rb +0 -9
  218. data/test/test_rdoc_markup_parser.rb +0 -1395
  219. data/test/test_rdoc_markup_pre_process.rb +0 -185
  220. data/test/test_rdoc_markup_raw.rb +0 -27
  221. data/test/test_rdoc_markup_to_ansi.rb +0 -328
  222. data/test/test_rdoc_markup_to_bs.rb +0 -341
  223. data/test/test_rdoc_markup_to_html.rb +0 -335
  224. data/test/test_rdoc_markup_to_html_crossref.rb +0 -169
  225. data/test/test_rdoc_markup_to_rdoc.rb +0 -327
  226. data/test/test_rdoc_method_attr.rb +0 -122
  227. data/test/test_rdoc_normal_class.rb +0 -17
  228. data/test/test_rdoc_normal_module.rb +0 -31
  229. data/test/test_rdoc_options.rb +0 -342
  230. data/test/test_rdoc_parser.rb +0 -83
  231. data/test/test_rdoc_parser_c.rb +0 -912
  232. data/test/test_rdoc_parser_ruby.rb +0 -1754
  233. data/test/test_rdoc_parser_simple.rb +0 -99
  234. data/test/test_rdoc_rdoc.rb +0 -164
  235. data/test/test_rdoc_require.rb +0 -25
  236. data/test/test_rdoc_ri_driver.rb +0 -846
  237. data/test/test_rdoc_ri_paths.rb +0 -43
  238. data/test/test_rdoc_ri_store.rb +0 -352
  239. data/test/test_rdoc_ruby_lex.rb +0 -23
  240. data/test/test_rdoc_stats.rb +0 -38
  241. data/test/test_rdoc_task.rb +0 -92
  242. data/test/test_rdoc_text.rb +0 -251
  243. data/test/test_rdoc_top_level.rb +0 -120
  244. data/test/xref_data.rb +0 -62
  245. data/test/xref_test_case.rb +0 -61
  246. data.tar.gz.sig +0 -3
  247. metadata.gz.sig +0 -0
@@ -1,11 +1,9 @@
1
+ # frozen_string_literal: true
1
2
  ##
2
- # Collection of methods for writing parsers against RDoc::RubyLex and
3
- # RDoc::RubyToken
3
+ # Collection of methods for writing parsers
4
4
 
5
5
  module RDoc::Parser::RubyTools
6
6
 
7
- include RDoc::RubyToken
8
-
9
7
  ##
10
8
  # Adds a token listener +obj+, but you should probably use token_listener
11
9
 
@@ -21,38 +19,24 @@ module RDoc::Parser::RubyTools
21
19
  tk = nil
22
20
 
23
21
  if @tokens.empty? then
24
- tk = @scanner.token
25
- @read.push @scanner.get_readed
26
- puts "get_tk1 => #{tk.inspect}" if $TOKEN_DEBUG
22
+ if @scanner_point >= @scanner.size
23
+ return nil
24
+ else
25
+ tk = @scanner[@scanner_point]
26
+ @scanner_point += 1
27
+ @read.push tk[:text]
28
+ end
27
29
  else
28
30
  @read.push @unget_read.shift
29
31
  tk = @tokens.shift
30
- puts "get_tk2 => #{tk.inspect}" if $TOKEN_DEBUG
31
32
  end
32
33
 
33
- tk = nil if TkEND_OF_SCRIPT === tk
34
-
35
- if TkSYMBEG === tk then
36
- set_token_position tk.line_no, tk.char_no
37
-
38
- case tk1 = get_tk
39
- when TkId, TkOp, TkSTRING, TkDSTRING, TkSTAR, TkAMPER then
40
- if tk1.respond_to?(:name) then
41
- tk = Token(TkSYMBOL).set_text(":" + tk1.name)
42
- else
43
- tk = Token(TkSYMBOL).set_text(":" + tk1.text)
44
- end
45
-
46
- # remove the identifier we just read (we're about to replace it with a
47
- # symbol)
48
- @token_listeners.each do |obj|
49
- obj.pop_token
50
- end if @token_listeners
51
- else
52
- tk = tk1
53
- end
34
+ if tk == nil || :on___end__ == tk[:kind]
35
+ tk = nil
54
36
  end
55
37
 
38
+ return nil unless tk
39
+
56
40
  # inform any listeners of our shiny new token
57
41
  @token_listeners.each do |obj|
58
42
  obj.add_token(tk)
@@ -70,7 +54,13 @@ module RDoc::Parser::RubyTools
70
54
 
71
55
  loop do
72
56
  tk = get_tk
73
- case tk when *tokens then unget_tk tk; break end
57
+
58
+ case tk
59
+ when *tokens then
60
+ unget_tk tk
61
+ break
62
+ end
63
+
74
64
  read << tk
75
65
  end
76
66
 
@@ -116,19 +106,34 @@ module RDoc::Parser::RubyTools
116
106
  @tokens = []
117
107
  @unget_read = []
118
108
  @nest = 0
109
+ @scanner_point = 0
119
110
  end
120
111
 
121
112
  ##
122
- # Skips whitespace tokens including newlines if +skip_nl+ is true
113
+ # Skips whitespace tokens including newlines
123
114
 
124
- def skip_tkspace(skip_nl = true) # HACK dup
115
+ def skip_tkspace
125
116
  tokens = []
126
117
 
127
- while TkSPACE === (tk = get_tk) or (skip_nl and TkNL === tk) do
128
- tokens.push tk
118
+ while (tk = get_tk) and (:on_sp == tk[:kind] or :on_nl == tk[:kind] or :on_ignored_nl == tk[:kind]) do
119
+ tokens.push(tk)
129
120
  end
130
121
 
131
- unget_tk tk
122
+ unget_tk(tk)
123
+ tokens
124
+ end
125
+
126
+ ##
127
+ # Skips whitespace tokens excluding newlines
128
+
129
+ def skip_tkspace_without_nl
130
+ tokens = []
131
+
132
+ while (tk = get_tk) and :on_sp == tk[:kind] do
133
+ tokens.push(tk)
134
+ end
135
+
136
+ unget_tk(tk)
132
137
  tokens
133
138
  end
134
139
 
@@ -153,6 +158,8 @@ module RDoc::Parser::RubyTools
153
158
  @token_listeners.each do |obj|
154
159
  obj.pop_token
155
160
  end if @token_listeners
161
+
162
+ nil
156
163
  end
157
164
 
158
165
  end
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
1
2
  ##
2
3
  # Parse a non-source file. We basically take the whole thing as one big
3
4
  # comment.
4
5
 
5
6
  class RDoc::Parser::Simple < RDoc::Parser
6
7
 
8
+ include RDoc::Parser::Text
9
+
7
10
  parse_files_matching(//)
8
11
 
9
12
  attr_reader :content # :nodoc:
@@ -16,7 +19,7 @@ class RDoc::Parser::Simple < RDoc::Parser
16
19
 
17
20
  preprocess = RDoc::Markup::PreProcess.new @file_name, @options.rdoc_include
18
21
 
19
- preprocess.handle @content, @top_level
22
+ @content = preprocess.handle @content, @top_level
20
23
  end
21
24
 
22
25
  ##
@@ -24,20 +27,14 @@ class RDoc::Parser::Simple < RDoc::Parser
24
27
 
25
28
  def scan
26
29
  comment = remove_coding_comment @content
27
- comment = remove_private_comments comment
30
+ comment = remove_private_comment comment
31
+
32
+ comment = RDoc::Comment.new comment, @top_level
28
33
 
29
34
  @top_level.comment = comment
30
- @top_level.parser = self.class
31
35
  @top_level
32
36
  end
33
37
 
34
- ##
35
- # Removes comments wrapped in <tt>--/++</tt>
36
-
37
- def remove_private_comments text
38
- text.gsub(/^--\n.*?^\+\+/m, '').sub(/^--\n.*/m, '')
39
- end
40
-
41
38
  ##
42
39
  # Removes the encoding magic comment from +text+
43
40
 
@@ -45,5 +42,20 @@ class RDoc::Parser::Simple < RDoc::Parser
45
42
  text.sub(/\A# .*coding[=:].*$/, '')
46
43
  end
47
44
 
48
- end
45
+ ##
46
+ # Removes private comments.
47
+ #
48
+ # Unlike RDoc::Comment#remove_private this implementation only looks for two
49
+ # dashes at the beginning of the line. Three or more dashes are considered
50
+ # to be a rule and ignored.
51
+
52
+ def remove_private_comment comment
53
+ # Workaround for gsub encoding for Ruby 1.9.2 and earlier
54
+ empty = ''
55
+ empty = RDoc::Encoding.change_encoding empty, comment.encoding
56
+
57
+ comment = comment.gsub(%r%^--\n.*?^\+\+\n?%m, empty)
58
+ comment.sub(%r%^--\n.*%m, empty)
59
+ end
49
60
 
61
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ ##
3
+ # Indicates this parser is text and doesn't contain code constructs.
4
+ #
5
+ # Include this module in a RDoc::Parser subclass to make it show up as a file,
6
+ # not as part of a class or module.
7
+ #--
8
+ # This is not named File to avoid overriding ::File
9
+
10
+ module RDoc::Parser::Text
11
+ end
12
+
data/lib/rdoc/parser.rb CHANGED
@@ -1,42 +1,34 @@
1
- require 'rdoc'
2
- require 'rdoc/code_objects'
3
- require 'rdoc/markup/pre_process'
4
- require 'rdoc/stats'
1
+ # -*- coding: us-ascii -*-
2
+ # frozen_string_literal: true
5
3
 
6
4
  ##
7
- # A parser is simple a class that implements
5
+ # A parser is simple a class that subclasses RDoc::Parser and implements #scan
6
+ # to fill in an RDoc::TopLevel with parsed data.
8
7
  #
9
- # #initialize(file_name, body, options)
8
+ # The initialize method takes an RDoc::TopLevel to fill with parsed content,
9
+ # the name of the file to be parsed, the content of the file, an RDoc::Options
10
+ # object and an RDoc::Stats object to inform the user of parsed items. The
11
+ # scan method is then called to parse the file and must return the
12
+ # RDoc::TopLevel object. By calling super these items will be set for you.
10
13
  #
11
- # and
14
+ # In order to be used by RDoc the parser needs to register the file extensions
15
+ # it can parse. Use ::parse_files_matching to register extensions.
12
16
  #
13
- # #scan
14
- #
15
- # The initialize method takes a file name to be used, the body of the file,
16
- # and an RDoc::Options object. The scan method is then called to return an
17
- # appropriately parsed TopLevel code object.
18
- #
19
- # The ParseFactory is used to redirect to the correct parser given a
20
- # filename extension. This magic works because individual parsers have to
21
- # register themselves with us as they are loaded in. The do this using the
22
- # following incantation
23
- #
24
- # require "rdoc/parser"
17
+ # require 'rdoc'
25
18
  #
26
19
  # class RDoc::Parser::Xyz < RDoc::Parser
27
- # parse_files_matching /\.xyz$/ # <<<<
20
+ # parse_files_matching /\.xyz$/
28
21
  #
29
- # def initialize(file_name, body, options)
30
- # ...
22
+ # def initialize top_level, file_name, content, options, stats
23
+ # super
24
+ #
25
+ # # extra initialization if needed
31
26
  # end
32
27
  #
33
28
  # def scan
34
- # ...
29
+ # # parse file and fill in @top_level
35
30
  # end
36
31
  # end
37
- #
38
- # Just to make life interesting, if we suspect a plain text file, we also
39
- # look for a shebang line just in case it's a potential shell script
40
32
 
41
33
  class RDoc::Parser
42
34
 
@@ -45,8 +37,8 @@ class RDoc::Parser
45
37
  class << self
46
38
 
47
39
  ##
48
- # A Hash that maps file exetensions regular expressions to parsers that
49
- # will consume them.
40
+ # An Array of arrays that maps file extension (or name) regular
41
+ # expressions to parser classes that will parse matching filenames.
50
42
  #
51
43
  # Use parse_files_matching to register a parser's file extensions.
52
44
 
@@ -54,6 +46,11 @@ class RDoc::Parser
54
46
 
55
47
  end
56
48
 
49
+ ##
50
+ # The name of the file being parsed
51
+
52
+ attr_reader :file_name
53
+
57
54
  ##
58
55
  # Alias an extension to another extension. After this call, files ending
59
56
  # "new_ext" will be parsed using the same parser as "old_ext"
@@ -62,7 +59,7 @@ class RDoc::Parser
62
59
  old_ext = old_ext.sub(/^\.(.*)/, '\1')
63
60
  new_ext = new_ext.sub(/^\.(.*)/, '\1')
64
61
 
65
- parser = can_parse "xxx.#{old_ext}"
62
+ parser = can_parse_by_name "xxx.#{old_ext}"
66
63
  return false unless parser
67
64
 
68
65
  RDoc::Parser.parsers.unshift [/\.#{new_ext}$/, parser]
@@ -79,48 +76,15 @@ class RDoc::Parser
79
76
 
80
77
  s = File.read(file, 1024) or return false
81
78
 
82
- have_encoding = s.respond_to? :encoding
83
-
84
- if have_encoding then
85
- return false if s.encoding != Encoding::ASCII_8BIT and s.valid_encoding?
86
- end
87
-
88
79
  return true if s[0, 2] == Marshal.dump('')[0, 2] or s.index("\x00")
89
80
 
90
- if have_encoding then
91
- s.force_encoding Encoding.default_external
81
+ mode = 'r:utf-8' # default source encoding has been changed to utf-8
82
+ s.sub!(/\A#!.*\n/, '') # assume shebang line isn't longer than 1024.
83
+ encoding = s[/^\s*\#\s*(?:-\*-\s*)?(?:en)?coding:\s*([^\s;]+?)(?:-\*-|[\s;])/, 1]
84
+ mode = "rb:#{encoding}" if encoding
85
+ s = File.open(file, mode) {|f| f.gets(nil, 1024)}
92
86
 
93
- not s.valid_encoding?
94
- else
95
- if 0.respond_to? :fdiv then
96
- s.count("\x00-\x7F", "^ -~\t\r\n").fdiv(s.size) > 0.3
97
- else # HACK 1.8.6
98
- (s.count("\x00-\x7F", "^ -~\t\r\n").to_f / s.size) > 0.3
99
- end
100
- end
101
- end
102
-
103
- ##
104
- # Processes common directives for CodeObjects for the C and Ruby parsers.
105
- #
106
- # Applies +directive+'s +value+ to +code_object+, if appropriate
107
-
108
- def self.process_directive code_object, directive, value
109
- case directive
110
- when 'nodoc' then
111
- code_object.document_self = nil # notify nodoc
112
- code_object.document_children = value.downcase != 'all'
113
- when 'doc' then
114
- code_object.document_self = true
115
- code_object.force_documentation = true
116
- when 'yield', 'yields' then
117
- # remove parameter &block
118
- code_object.params.sub!(/,?\s*&\w+/, '') if code_object.params
119
-
120
- code_object.block_params = value
121
- when 'arg', 'args' then
122
- code_object.params = value
123
- end
87
+ not s.valid_encoding?
124
88
  end
125
89
 
126
90
  ##
@@ -133,46 +97,100 @@ class RDoc::Parser
133
97
  zip_signature == "PK\x03\x04" or
134
98
  zip_signature == "PK\x05\x06" or
135
99
  zip_signature == "PK\x07\x08"
100
+ rescue
101
+ false
136
102
  end
137
103
 
138
104
  ##
139
105
  # Return a parser that can handle a particular extension
140
106
 
141
- def self.can_parse(file_name)
142
- parser = RDoc::Parser.parsers.find { |regexp,| regexp =~ file_name }.last
107
+ def self.can_parse file_name
108
+ parser = can_parse_by_name file_name
143
109
 
144
110
  # HACK Selenium hides a jar file using a .txt extension
145
111
  return if parser == RDoc::Parser::Simple and zip? file_name
146
112
 
113
+ parser
114
+ end
115
+
116
+ ##
117
+ # Returns a parser that can handle the extension for +file_name+. This does
118
+ # not depend upon the file being readable.
119
+
120
+ def self.can_parse_by_name file_name
121
+ _, parser = RDoc::Parser.parsers.find { |regexp,| regexp =~ file_name }
122
+
147
123
  # The default parser must not parse binary files
148
124
  ext_name = File.extname file_name
149
125
  return parser if ext_name.empty?
150
- return if parser == RDoc::Parser::Simple and ext_name !~ /txt|rdoc/
126
+
127
+ if parser == RDoc::Parser::Simple and ext_name !~ /txt|rdoc/ then
128
+ case check_modeline file_name
129
+ when nil, 'rdoc' then # continue
130
+ else return nil
131
+ end
132
+ end
151
133
 
152
134
  parser
135
+ rescue Errno::EACCES
136
+ end
137
+
138
+ ##
139
+ # Returns the file type from the modeline in +file_name+
140
+
141
+ def self.check_modeline file_name
142
+ line = File.open file_name do |io|
143
+ io.gets
144
+ end
145
+
146
+ /-\*-\s*(.*?\S)\s*-\*-/ =~ line
147
+
148
+ return nil unless type = $1
149
+
150
+ if /;/ =~ type then
151
+ return nil unless /(?:\s|\A)mode:\s*([^\s;]+)/i =~ type
152
+ type = $1
153
+ end
154
+
155
+ return nil if /coding:/i =~ type
156
+
157
+ type.downcase
158
+ rescue ArgumentError
159
+ rescue Encoding::InvalidByteSequenceError # invalid byte sequence
160
+
153
161
  end
154
162
 
155
163
  ##
156
- # Find the correct parser for a particular file name. Return a SimpleParser
157
- # for ones that we don't know
164
+ # Finds and instantiates the correct parser for the given +file_name+ and
165
+ # +content+.
158
166
 
159
- def self.for(top_level, file_name, body, options, stats)
167
+ def self.for top_level, file_name, content, options, stats
160
168
  return if binary? file_name
161
169
 
162
- # If no extension, look for shebang
163
- if file_name !~ /\.\w+$/ && body =~ %r{\A#!(.+)} then
164
- shebang = $1
165
- case shebang
166
- when %r{env\s+ruby}, %r{/ruby}
167
- file_name = "dummy.rb"
170
+ parser = use_markup content
171
+
172
+ unless parser then
173
+ parse_name = file_name
174
+
175
+ # If no extension, look for shebang
176
+ if file_name !~ /\.\w+$/ && content =~ %r{\A#!(.+)} then
177
+ shebang = $1
178
+ case shebang
179
+ when %r{env\s+ruby}, %r{/ruby}
180
+ parse_name = 'dummy.rb'
181
+ end
168
182
  end
169
- end
170
183
 
171
- parser = can_parse file_name
184
+ parser = can_parse parse_name
185
+ end
172
186
 
173
187
  return unless parser
174
188
 
175
- parser.new top_level, file_name, body, options, stats
189
+ content = remove_modeline content
190
+
191
+ parser.new top_level, file_name, content, options, stats
192
+ rescue SystemCallError
193
+ nil
176
194
  end
177
195
 
178
196
  ##
@@ -185,20 +203,75 @@ class RDoc::Parser
185
203
  end
186
204
 
187
205
  ##
188
- # Creates a new Parser storing +top_level+, +file_name+, +content+,
189
- # +options+ and +stats+ in instance variables.
206
+ # Removes an emacs-style modeline from the first line of the document
207
+
208
+ def self.remove_modeline content
209
+ content.sub(/\A.*-\*-\s*(.*?\S)\s*-\*-.*\r?\n/, '')
210
+ end
211
+
212
+ ##
213
+ # If there is a <tt>markup: parser_name</tt> comment at the front of the
214
+ # file, use it to determine the parser. For example:
215
+ #
216
+ # # markup: rdoc
217
+ # # Class comment can go here
218
+ #
219
+ # class C
220
+ # end
190
221
  #
191
- # Usually invoked by +super+
222
+ # The comment should appear as the first line of the +content+.
223
+ #
224
+ # If the content contains a shebang or editor modeline the comment may
225
+ # appear on the second or third line.
226
+ #
227
+ # Any comment style may be used to hide the markup comment.
228
+
229
+ def self.use_markup content
230
+ markup = content.lines.first(3).grep(/markup:\s+(\w+)/) { $1 }.first
231
+
232
+ return unless markup
233
+
234
+ # TODO Ruby should be returned only when the filename is correct
235
+ return RDoc::Parser::Ruby if %w[tomdoc markdown].include? markup
236
+
237
+ markup = Regexp.escape markup
238
+
239
+ _, selected = RDoc::Parser.parsers.find do |_, parser|
240
+ /^#{markup}$/i =~ parser.name.sub(/.*:/, '')
241
+ end
192
242
 
193
- def initialize(top_level, file_name, content, options, stats)
243
+ selected
244
+ end
245
+
246
+ ##
247
+ # Creates a new Parser storing +top_level+, +file_name+, +content+,
248
+ # +options+ and +stats+ in instance variables. In +@preprocess+ an
249
+ # RDoc::Markup::PreProcess object is created which allows processing of
250
+ # directives.
251
+
252
+ def initialize top_level, file_name, content, options, stats
194
253
  @top_level = top_level
254
+ @top_level.parser = self.class
255
+ @store = @top_level.store
256
+
195
257
  @file_name = file_name
196
258
  @content = content
197
259
  @options = options
198
260
  @stats = stats
261
+
262
+ @preprocess = RDoc::Markup::PreProcess.new @file_name, @options.rdoc_include
263
+ @preprocess.options = @options
199
264
  end
200
265
 
201
- end
266
+ autoload :RubyTools, 'rdoc/parser/ruby_tools'
267
+ autoload :Text, 'rdoc/parser/text'
202
268
 
203
- require 'rdoc/parser/simple'
269
+ end
204
270
 
271
+ # simple must come first in order to show up last in the parsers list
272
+ require_relative 'parser/simple'
273
+ require_relative 'parser/c'
274
+ require_relative 'parser/changelog'
275
+ require_relative 'parser/markdown'
276
+ require_relative 'parser/rd'
277
+ require_relative 'parser/ruby'