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
@@ -0,0 +1,202 @@
1
+ # frozen_string_literal: true
2
+ ##
3
+ # RDoc::CrossReference is a reusable way to create cross references for names.
4
+
5
+ class RDoc::CrossReference
6
+
7
+ ##
8
+ # Regular expression to match class references
9
+ #
10
+ # 1. There can be a '\\' in front of text to suppress the cross-reference
11
+ # 2. There can be a '::' in front of class names to reference from the
12
+ # top-level namespace.
13
+ # 3. The method can be followed by parenthesis (not recommended)
14
+
15
+ CLASS_REGEXP_STR = '\\\\?((?:\:{2})?[A-Z]\w*(?:\:\:\w+)*)'
16
+
17
+ ##
18
+ # Regular expression to match method references.
19
+ #
20
+ # See CLASS_REGEXP_STR
21
+
22
+ METHOD_REGEXP_STR = '([a-z]\w*[!?=]?|%|===|\[\]=?|<<|>>|\+@|-@|-|\+|\*)(?:\([\w.+*/=<>-]*\))?'
23
+
24
+ ##
25
+ # Regular expressions matching text that should potentially have
26
+ # cross-reference links generated are passed to add_regexp_handling. Note
27
+ # that these expressions are meant to pick up text for which cross-references
28
+ # have been suppressed, since the suppression characters are removed by the
29
+ # code that is triggered.
30
+
31
+ CROSSREF_REGEXP = /(?:^|[\s()])
32
+ (
33
+ (?:
34
+ # A::B::C.meth
35
+ #{CLASS_REGEXP_STR}(?:[.#]|::)#{METHOD_REGEXP_STR}
36
+
37
+ # Stand-alone method (preceded by a #)
38
+ | \\?\##{METHOD_REGEXP_STR}
39
+
40
+ # Stand-alone method (preceded by ::)
41
+ | ::#{METHOD_REGEXP_STR}
42
+
43
+ # A::B::C
44
+ # The stuff after CLASS_REGEXP_STR is a
45
+ # nasty hack. CLASS_REGEXP_STR unfortunately matches
46
+ # words like dog and cat (these are legal "class"
47
+ # names in Fortran 95). When a word is flagged as a
48
+ # potential cross-reference, limitations in the markup
49
+ # engine suppress other processing, such as typesetting.
50
+ # This is particularly noticeable for contractions.
51
+ # In order that words like "can't" not
52
+ # be flagged as potential cross-references, only
53
+ # flag potential class cross-references if the character
54
+ # after the cross-reference is a space, sentence
55
+ # punctuation, tag start character, or attribute
56
+ # marker.
57
+ | #{CLASS_REGEXP_STR}(?=[@\s).?!,;<\000]|\z)
58
+
59
+ # Things that look like filenames
60
+ # The key thing is that there must be at least
61
+ # one special character (period, slash, or
62
+ # underscore).
63
+ | (?:\.\.\/)*[-\/\w]+[_\/.][-\w\/.]+
64
+
65
+ # Things that have markup suppressed
66
+ # Don't process things like '\<' in \<tt>, though.
67
+ # TODO: including < is a hack, not very satisfying.
68
+ | \\[^\s<]
69
+ )
70
+
71
+ # labels for headings
72
+ (?:@[\w+%-]+(?:\.[\w|%-]+)?)?
73
+ )/x
74
+
75
+ ##
76
+ # Version of CROSSREF_REGEXP used when <tt>--hyperlink-all</tt> is specified.
77
+
78
+ ALL_CROSSREF_REGEXP = /
79
+ (?:^|[\s()])
80
+ (
81
+ (?:
82
+ # A::B::C.meth
83
+ #{CLASS_REGEXP_STR}(?:[.#]|::)#{METHOD_REGEXP_STR}
84
+
85
+ # Stand-alone method
86
+ | \\?#{METHOD_REGEXP_STR}
87
+
88
+ # A::B::C
89
+ | #{CLASS_REGEXP_STR}(?=[@\s).?!,;<\000]|\z)
90
+
91
+ # Things that look like filenames
92
+ | (?:\.\.\/)*[-\/\w]+[_\/.][-\w\/.]+
93
+
94
+ # Things that have markup suppressed
95
+ | \\[^\s<]
96
+ )
97
+
98
+ # labels for headings
99
+ (?:@[\w+%-]+)?
100
+ )/x
101
+
102
+ ##
103
+ # Hash of references that have been looked-up to their replacements
104
+
105
+ attr_accessor :seen
106
+
107
+ ##
108
+ # Allows cross-references to be created based on the given +context+
109
+ # (RDoc::Context).
110
+
111
+ def initialize context
112
+ @context = context
113
+ @store = context.store
114
+
115
+ @seen = {}
116
+ end
117
+
118
+ ##
119
+ # Returns a reference to +name+.
120
+ #
121
+ # If the reference is found and +name+ is not documented +text+ will be
122
+ # returned. If +name+ is escaped +name+ is returned. If +name+ is not
123
+ # found +text+ is returned.
124
+
125
+ def resolve name, text
126
+ return @seen[name] if @seen.include? name
127
+
128
+ if /#{CLASS_REGEXP_STR}([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then
129
+ type = $2
130
+ if '.' == type # will find either #method or ::method
131
+ method = $3
132
+ else
133
+ method = "#{type}#{$3}"
134
+ end
135
+ container = @context.find_symbol_module($1)
136
+ elsif /^([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then
137
+ type = $1
138
+ if '.' == type
139
+ method = $2
140
+ else
141
+ method = "#{type}#{$2}"
142
+ end
143
+ container = @context
144
+ else
145
+ type = nil
146
+ container = nil
147
+ end
148
+
149
+ if container then
150
+ unless RDoc::TopLevel === container then
151
+ if '.' == type then
152
+ if 'new' == method then # AnyClassName.new will be class method
153
+ ref = container.find_local_symbol method
154
+ ref = container.find_ancestor_local_symbol method unless ref
155
+ else
156
+ ref = container.find_local_symbol "::#{method}"
157
+ ref = container.find_ancestor_local_symbol "::#{method}" unless ref
158
+ ref = container.find_local_symbol "##{method}" unless ref
159
+ ref = container.find_ancestor_local_symbol "##{method}" unless ref
160
+ end
161
+ else
162
+ ref = container.find_local_symbol method
163
+ ref = container.find_ancestor_local_symbol method unless ref
164
+ end
165
+ end
166
+ end
167
+
168
+ ref = case name
169
+ when /^\\(#{CLASS_REGEXP_STR})$/o then
170
+ @context.find_symbol $1
171
+ else
172
+ @context.find_symbol name
173
+ end unless ref
174
+
175
+ # Try a page name
176
+ ref = @store.page name if not ref and name =~ /^[\w.]+$/
177
+
178
+ ref = nil if RDoc::Alias === ref # external alias, can't link to it
179
+
180
+ out = if name == '\\' then
181
+ name
182
+ elsif name =~ /^\\/ then
183
+ # we remove the \ only in front of what we know:
184
+ # other backslashes are treated later, only outside of <tt>
185
+ ref ? $' : name
186
+ elsif ref then
187
+ if ref.display? then
188
+ ref
189
+ else
190
+ text
191
+ end
192
+ else
193
+ text
194
+ end
195
+
196
+ @seen[name] = out
197
+
198
+ out
199
+ end
200
+
201
+ end
202
+
data/lib/rdoc/encoding.rb CHANGED
@@ -1,4 +1,5 @@
1
- require 'rdoc'
1
+ # coding: US-ASCII
2
+ # frozen_string_literal: true
2
3
 
3
4
  ##
4
5
  # This class is a wrapper around File IO and Encoding that helps RDoc load
@@ -6,42 +7,74 @@ require 'rdoc'
6
7
 
7
8
  module RDoc::Encoding
8
9
 
10
+ HEADER_REGEXP = /^
11
+ (?:
12
+ \A\#!.*\n
13
+ |
14
+ ^\#\s+frozen[-_]string[-_]literal[=:].+\n
15
+ |
16
+ ^\#[^\n]+\b(?:en)?coding[=:]\s*(?<name>[^\s;]+).*\n
17
+ |
18
+ <\?xml[^?]*encoding=(?<quote>["'])(?<name>.*?)\k<quote>.*\n
19
+ )+
20
+ /xi # :nodoc:
21
+
9
22
  ##
10
23
  # Reads the contents of +filename+ and handles any encoding directives in
11
24
  # the file.
12
25
  #
13
26
  # The content will be converted to the +encoding+. If the file cannot be
14
27
  # converted a warning will be printed and nil will be returned.
28
+ #
29
+ # If +force_transcode+ is true the document will be transcoded and any
30
+ # unknown character in the target encoding will be replaced with '?'
15
31
 
16
- def self.read_file filename, encoding
17
- content = open filename, "rb" do |f| f.read end
32
+ def self.read_file filename, encoding, force_transcode = false
33
+ content = File.open filename, "rb" do |f| f.read end
34
+ content.gsub!("\r\n", "\n") if RUBY_PLATFORM =~ /mswin|mingw/
18
35
 
19
36
  utf8 = content.sub!(/\A\xef\xbb\xbf/, '')
20
37
 
21
- RDoc::Encoding.set_encoding content
38
+ enc = RDoc::Encoding.detect_encoding content
39
+ content = RDoc::Encoding.change_encoding content, enc if enc
22
40
 
23
- if Object.const_defined? :Encoding then
41
+ begin
24
42
  encoding ||= Encoding.default_external
25
43
  orig_encoding = content.encoding
26
44
 
27
- if utf8 then
28
- content.force_encoding Encoding::UTF_8
29
- content.encode! encoding
45
+ if not orig_encoding.ascii_compatible? then
46
+ content = content.encode encoding
47
+ elsif utf8 then
48
+ content = RDoc::Encoding.change_encoding content, Encoding::UTF_8
49
+ content = content.encode encoding
30
50
  else
31
51
  # assume the content is in our output encoding
32
- content.force_encoding encoding
52
+ content = RDoc::Encoding.change_encoding content, encoding
33
53
  end
34
54
 
35
55
  unless content.valid_encoding? then
36
56
  # revert and try to transcode
37
- content.force_encoding orig_encoding
38
- content.encode! encoding
57
+ content = RDoc::Encoding.change_encoding content, orig_encoding
58
+ content = content.encode encoding
39
59
  end
40
60
 
41
61
  unless content.valid_encoding? then
42
62
  warn "unable to convert #{filename} to #{encoding}, skipping"
43
63
  content = nil
44
64
  end
65
+ rescue Encoding::InvalidByteSequenceError,
66
+ Encoding::UndefinedConversionError => e
67
+ if force_transcode then
68
+ content = RDoc::Encoding.change_encoding content, orig_encoding
69
+ content = content.encode(encoding,
70
+ :invalid => :replace,
71
+ :undef => :replace,
72
+ :replace => '?')
73
+ return content
74
+ else
75
+ warn "unable to convert #{e.message} for #{filename}, skipping"
76
+ return nil
77
+ end
45
78
  end
46
79
 
47
80
  content
@@ -49,33 +82,55 @@ module RDoc::Encoding
49
82
  raise unless e.message =~ /unknown encoding name - (.*)/
50
83
  warn "unknown encoding name \"#{$1}\" for #{filename}, skipping"
51
84
  nil
52
- rescue Encoding::UndefinedConversionError => e
53
- warn "unable to convert #{e.message} for #{filename}, skipping"
54
- nil
55
85
  rescue Errno::EISDIR, Errno::ENOENT
56
86
  nil
57
87
  end
58
88
 
59
- ##
60
- # Sets the encoding of +string+ based on the magic comment
89
+ def self.remove_frozen_string_literal string
90
+ string =~ /\A(?:#!.*\n)?(.*\n)/
91
+ first_line = $1
61
92
 
62
- def self.set_encoding string
63
- first_line = string[/\A(?:#!.*\n)?.*\n/]
93
+ if first_line =~ /\A# +frozen[-_]string[-_]literal[=:].+$/i
94
+ string = string.sub first_line, ''
95
+ end
64
96
 
65
- name = case first_line
66
- when /^<\?xml[^?]*encoding=(["'])(.*?)\1/ then $2
67
- when /\b(?:en)?coding[=:]\s*([^\s;]+)/i then $1
68
- else return
69
- end
97
+ string
98
+ end
70
99
 
71
- string.sub! first_line, ''
100
+ ##
101
+ # Detects the encoding of +string+ based on the magic comment
72
102
 
73
- return unless Object.const_defined? :Encoding
103
+ def self.detect_encoding string
104
+ result = HEADER_REGEXP.match string
105
+ name = result && result[:name]
74
106
 
75
- enc = Encoding.find name
76
- string.force_encoding enc if enc
107
+ name ? Encoding.find(name) : nil
77
108
  end
78
109
 
79
- end
110
+ ##
111
+ # Removes magic comments and shebang
80
112
 
113
+ def self.remove_magic_comment string
114
+ string.sub HEADER_REGEXP do |s|
115
+ s.gsub(/[^\n]/, '')
116
+ end
117
+ end
118
+
119
+ ##
120
+ # Changes encoding based on +encoding+ without converting and returns new
121
+ # string
122
+
123
+ def self.change_encoding text, encoding
124
+ if text.kind_of? RDoc::Comment
125
+ text.encode! encoding
126
+ else
127
+ # TODO: Remove this condition after Ruby 2.2 EOL
128
+ if RUBY_VERSION < '2.3.0'
129
+ text.force_encoding encoding
130
+ else
131
+ String.new text, encoding: encoding
132
+ end
133
+ end
134
+ end
81
135
 
136
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ ##
3
+ # Allows an ERB template to be rendered in the context (binding) of an
4
+ # existing ERB template evaluation.
5
+
6
+ class RDoc::ERBPartial < ERB
7
+
8
+ ##
9
+ # Overrides +compiler+ startup to set the +eoutvar+ to an empty string only
10
+ # if it isn't already set.
11
+
12
+ def set_eoutvar compiler, eoutvar = '_erbout'
13
+ super
14
+
15
+ compiler.pre_cmd = ["#{eoutvar} ||= +''"]
16
+ end
17
+
18
+ end
19
+
data/lib/rdoc/erbio.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'erb'
2
3
 
3
4
  ##
@@ -8,7 +9,7 @@ require 'erb'
8
9
  #
9
10
  # erbio = RDoc::ERBIO.new '<%= "hello world" %>', nil, nil
10
11
  #
11
- # open 'hello.txt', 'w' do |io|
12
+ # File.open 'hello.txt', 'w' do |io|
12
13
  # erbio.result binding
13
14
  # end
14
15
  #
@@ -19,8 +20,12 @@ class RDoc::ERBIO < ERB
19
20
  ##
20
21
  # Defaults +eoutvar+ to 'io', otherwise is identical to ERB's initialize
21
22
 
22
- def initialize str, safe_level = nil, trim_mode = nil, eoutvar = 'io'
23
- super
23
+ def initialize str, safe_level = nil, legacy_trim_mode = nil, legacy_eoutvar = 'io', trim_mode: nil, eoutvar: 'io'
24
+ if RUBY_VERSION >= '2.6'
25
+ super(str, trim_mode: trim_mode, eoutvar: eoutvar)
26
+ else
27
+ super(str, safe_level, legacy_trim_mode, legacy_eoutvar)
28
+ end
24
29
  end
25
30
 
26
31
  ##
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ ##
3
+ # A Module extension to a class with \#extend
4
+ #
5
+ # RDoc::Extend.new 'Enumerable', 'comment ...'
6
+
7
+ class RDoc::Extend < RDoc::Mixin
8
+
9
+ end
10
+