Class: RDoc::OSXDictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc_osx_dictionary.rb

Constant Summary collapse

VERSION =
'2.1.0'
EXCLUDE =
{
}
NAME_MAP =
{
  '!'   => 'bang',
  '%'   => 'percent',
  '&'   => 'and',
  '*'   => 'times',
  '**'  => 'times2',
  '+'   => 'plus',
  '-'   => 'minus',
  '/'   => 'div',
  '<'   => 'lt',
  '<='  => 'lte',
  '<=>' => 'spaceship',
  "<\<" => 'lt2',
  '=='  => 'equals2',
  '===' => 'equals3',
  '=~'  => 'equalstilde',
  '>'   => 'gt',
  '>='  => 'ge',
  '>>'  => 'gt2',
  '+@'  => 'unary_plus',
  '-@'  => 'unary_minus',
  '[]'  => 'idx',
  '[]=' => 'idx_equals',
  '^'   => 'carat',
  '|'   => 'or',
  '~'   => 'tilde',
  '='   => 'eq',
  '?'   => 'eh',
  '`'   => 'backtick',
  '::'  => '__',
}
NAME_MAP_RE =
Regexp.new(NAME_MAP.keys.sort_by { |k| k.length }.map {|s|
  Regexp.escape(s)
}.reverse.join("|"))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary_development_kit_dir) ⇒ OSXDictionary

Returns a new instance of OSXDictionary.



60
61
62
63
# File 'lib/rdoc_osx_dictionary.rb', line 60

def initialize(dictionary_development_kit_dir)
  @dictionary_development_kit_dir = dictionary_development_kit_dir
  @to_html = RDoc::Markup::ToHtml.new(RDoc::Options.new)
end

Instance Attribute Details

#to_htmlObject (readonly)

Returns the value of attribute to_html.



58
59
60
# File 'lib/rdoc_osx_dictionary.rb', line 58

def to_html
  @to_html
end

Class Method Details

.install_gem_hooksObject



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rdoc_osx_dictionary.rb', line 343

def self.install_gem_hooks
  return if @hooked[:hook]
  return unless File.exist? File.expand_path("~/.ri/autorun")

  rdoc_osx_dictionary_path = File.expand_path File.join(__FILE__, "../../bin/rdoc_osx_dictionary")
  cmd = "#{Gem.ruby} #{rdoc_osx_dictionary_path}"

  # post_install isn't actually fully post-install... so I must
  # force via at_exit :(
  Gem.post_install do |i|
    at_exit do
      next if @hooked[:install]
      @hooked[:install] = true
      warn "updating OSX ruby + gem dictionary, if necessary"
      system cmd
    end
  end

  Gem.post_uninstall do |i|
    at_exit do
      next if @hooked[:uninstall]
      @hooked[:uninstall] = true
      require 'fileutils'
      warn "nuking old ri cache to force rebuild"
      FileUtils.rm_r File.expand_path("~/.ri/dict")
      system cmd
    end
  end

  @hooked[:hook] = true
end

Instance Method Details

#class_details(name, fullname, definition, level = 2) ⇒ Object

REFACTOR: fold this back in



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rdoc_osx_dictionary.rb', line 141

def class_details name, fullname, definition, level = 2
  h         = "h#{level}"
  result    = []
  includes  = definition.includes.map { |c| c.name }
  constants = definition.constants.map { |c| c.name }

  classmeths = definition.singleton_methods.map { |cm|
    name = cm.name
    "<a href=\"x-dictionary:r:#{id "defs", fullname, name}\">#{name}</a>"
  }

  instmeths = definition.instance_method_list.map { |im|
    name = im.name
    "<a href=\"x-dictionary:r:#{id "def", fullname, name}\">#{name.munge}</a>"
  }

  [["Includes",         includes],
   ["Constants",        constants],
   ["Class Methods",    classmeths],
   ["Instance Methods", instmeths],
  ].each do |n, a|
    next if a.empty?

    result << "<#{h}>#{n}:</#{h}><p>#{a.join ", "}</p>"
  end

  result
end


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rdoc_osx_dictionary.rb', line 215

def d_footer classes, sources
  return <<-"EOD".gsub(/^ {6}/, '')
    <d:entry id="front_back_matter" d:title="Front/Back Matter">
      <h1><b>RubyGems Dictionary</b></h1>

      <div>
        Provides dictionary definitions for ruby core, stdlib, and
        all known installed ruby gems.
      </div>

      <h3>Sources:</h3>
      <div>#{sources.keys.sort.join ", "}</div>

      <h3>Classes:</h3>
      <div>#{classes.keys.sort.join ", "}</div>
    </d:entry>
    </d:dictionary>
  EOD
end

#d_headerObject



208
209
210
211
212
213
# File 'lib/rdoc_osx_dictionary.rb', line 208

def d_header
  return <<-"EOD".gsub(/^ {6}/, '')
    <?xml version="1.0" encoding="UTF-8"?>
    <d:dictionary xmlns="http://www.w3.org/1999/xhtml" xmlns:d="http://www.apple.com/DTDs/DictionaryService-1.0.rng">
  EOD
end

#d_signatures(name, params) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/rdoc_osx_dictionary.rb', line 199

def d_signatures name, params
  result = " "
  params ||= ""
  if params.strip =~ /^\(/
    result << "<b>#{name.munge}</b>"
  end
  result << "<b>#{params.munge.gsub(/\n+/, "\n")}</b>"
end

#display_method_info(definition, from, key) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rdoc_osx_dictionary.rb', line 170

def display_method_info definition, from, key
  fullname  = definition.full_name
  name      = definition.name
  params    = definition.arglists
  comment   = to_html.convert definition.comment

  return if name =~ /_reduce_\d+/

  comment = "undocumented" if comment.empty?
  comment = "Improperly formatted" if EXCLUDE[fullname]

  # HACK to deal with the Dictionary compiler fucking with my whitespace.
  comment = comment.gsub(/<span[^>]+>/, '').gsub(/<\/span>/, '')
  comment = comment.gsub(/(<pre[^>]*>)\s*\n/, '\1')

  return <<-"EOD".gsub(/^ {6}/, '')
    <d:entry id="#{key}" d:title="#{fullname.munge}">
      <d:index d:value="#{fullname.munge}"/>
      <d:index d:value="#{name.munge}"/>
      <h1>#{fullname.munge}</h1>
      <h3>From: #{from}</h3>
      <pre class="signatures">
        #{d_signatures(name, params)}
      </pre>
    #{comment}
    </d:entry>
  EOD
end

#id(*args) ⇒ Object



65
66
67
68
69
# File 'lib/rdoc_osx_dictionary.rb', line 65

def id *args
  args.map { |s| s.gsub(/:/, ',') }.join(",").gsub(/#{NAME_MAP_RE}/) { |x|
    ",#{NAME_MAP[x]}"
  }
end

#makeObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/rdoc_osx_dictionary.rb', line 235

def make
  base  = File.expand_path "~/.ri/"

  FileUtils.rm_rf base if $d

  dir = File.join base, "dict"
  FileUtils.mkdir_p dir unless File.directory? dir

  dirty = false
  force = $f || false
  ri    = RDoc::RI::Driver.new
  dict  = ri.classes

  l_seen = {}

  dict.sort.each do |klass, stores|
    path = "#{base}/dict/#{klass.downcase}.xml"

    next if $q and klass !~ /^(String|Array|Bignum)/

    unless File.exist? path then
      unless dirty then
        warn "New entries for dictionary. Rebuilding dictionary."
        warn "Sing along, kids!"
      end
      dirty = true
      warn klass if $v

      $stderr.print klass[0,1] unless l_seen[klass[0,1]]
      l_seen[klass[0,1]] = true

      write_class_info klass, stores, path
    end
  end

  warn "! YAY!! All done!!!" if dirty

  return unless dirty unless force

  seen = {}

  classes = {}
  sources = {}
  ri.classes.sort.each do |klass, stores|
    classes[klass] = true

    stores.each do |store|
      sources[store.friendly_path] = true
    end
  end

  dict_src_path = "#{base}/RubyGemsDictionary.xml"

  File.open(dict_src_path, "w") do |xml|
    xml.puts d_header

    dict.sort.each do |klass, stores|
      next if $q and klass !~ /^(String|Array|Bignum)/

      next if seen[klass.downcase]
      seen[klass.downcase] = true

      path = "#{base}/dict/#{klass.downcase}.xml"

      body = File.read path rescue nil
      if body then
        xml.puts body
      else
        warn "Skipping: couldn't read: #{path}"
      end
    end

    xml.puts d_footer(classes, sources)
  end

  dict_name = "RubyAndGems"
  data      = File.expand_path("#{__FILE__}/../../data")
  dict_path = File.expand_path "~/Library/Dictionaries"

  Dir.chdir base do
    build_dict = "#{@dictionary_development_kit_dir}/bin/build_dict.sh"
    unless File.exist?(build_dict)
      fail "Specify the directory containing bin/build_dict.sh."
    end
    run(build_dict, "-c=0", dict_name, dict_src_path,
        "#{data}/RubyGemsDictionary.css",
        "#{data}/RubyGemsInfo.plist")
  end

  warn "installing"

  FileUtils.mkdir_p dict_path

  run "rsync", "-r", "#{base}/objects/#{dict_name}.dictionary", dict_path

  FileUtils.touch dict_path

  warn "installed"
  warn "Run Dictionary.app to use the new dictionary. (activate in prefs!)"
end

#run(*cmd) ⇒ Object



336
337
338
339
# File 'lib/rdoc_osx_dictionary.rb', line 336

def run(*cmd)
  warn "running: " + cmd.map { |s| s.inspect }.join(" ") if $v
  abort "command failed" unless system(*cmd)
end

#write_class_info(klass, stores, path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rdoc_osx_dictionary.rb', line 71

def write_class_info klass, stores, path
  m_seen = {} # HACK: known issue: on a case insensitive FS we're losing files

  File.open(path, "w") do |f|
    m_result = []
    m_seen.clear

    fullname  = klass

    f.puts <<-"EOD".gsub(/^ {6}/, '')
      <d:entry id="#{id fullname}" d:title="#{fullname}">
        <d:index d:value="#{fullname.munge}"/>
    EOD

    first = true

    stores.each do |store|
      cdesc     = store.load_class klass
      type      = store.type
      from      = store.friendly_path
      name      = cdesc.name
      is_class  = ! cdesc.module?
      supername = cdesc.superclass if is_class
      comment   = to_html.convert cdesc.comment
      type      = is_class ? "class" : "module"
      title     = is_class ? "class #{fullname}" : "module #{fullname}"

      title += " < #{supername}" if is_class and supername != "Object"

      comment = "Improperly formatted" if EXCLUDE[fullname]

      shortname = "<d:index d:value=#{name.munge.inspect}/>" if name != fullname

      f.puts <<-"EOD".gsub(/^ {8}/, '')
          #{shortname}
          <h1>#{title.munge}</h1>
          <h3>From: #{from}</h3>

          #{comment}
      EOD

      level = first ? 2 : 4
      first = false

      f.puts class_details(name, fullname, cdesc, level)

      cdesc.method_list.each do |method|
        fullname = method.full_name
        key      = id from, fullname

        next if m_seen[key]

        method = store.load_method klass, fullname

        # HACK: after load_method, parent_name is not full name, pass down key
        m_result << display_method_info(method, from, key)
        m_seen[key] = true
      end
    end # stores.each

    f.puts <<-"EOD".gsub(/^ {6}/, '')
      </d:entry>

    EOD

    f.puts m_result.join("\n\n")
  end
end