Class: RDoc::Generator::Dictionary

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

Constant Summary collapse

XMLNS =
'http://www.w3.org/1999/xhtml'
XMLNS_D =
'http://www.apple.com/DTDs/DictionaryService-1.0.rng'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Dictionary

Returns a new instance of Dictionary.



41
42
43
44
45
46
47
48
# File 'lib/rubydictionary/generator.rb', line 41

def initialize(options)
  @options = options
  @template_dir = Pathname.new(File.expand_path('../../rdoc/generator/template', __FILE__))
  
  # Keep the track of methods and classes already rendered and avoid duplication
  @class_ids = []
  @method_ids = []
end

Class Method Details

.setup_options(options) ⇒ Object

TODO: Raise an error when dictionary name is missing



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubydictionary/generator.rb', line 25

def self.setup_options(options)
  opt = options.option_parser
  opt.separator "Dictionary generator options:"
  opt.separator nil
  opt.on('--dict-name=NAME', 'Title that appears in Dictionary.app') do |value|
    options.dictionary_name = value
  end
  opt.on('--dict-id=IDENTIFIER', 'Dictionary bundle identifier, such as', 'org.rubyonrails.Rails') do |value|
    options.dictionary_identifier = value
  end
  opt.on('--kit-path=DICTIONARY_KIT_PATH', 'Full path to Dictionary Development Kit') do |value|
    options.kit_path = value
  end
  opt.separator nil
end

Instance Method Details

#class_dirObject



91
92
93
# File 'lib/rubydictionary/generator.rb', line 91

def class_dir
  'classes'
end

#file_dirObject



87
88
89
# File 'lib/rubydictionary/generator.rb', line 87

def file_dir
  ''
end

#generate(top_levels) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubydictionary/generator.rb', line 50

def generate(top_levels)
  builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
    xml.send('dictionary', 'xmlns' => XMLNS, 'xmlns:d' => XMLNS_D) do
      xml.parent.namespace = xml.parent.add_namespace_definition('d', XMLNS_D)
      
      RDoc::TopLevel.all_classes_and_modules.each do |clazz|
        unless @class_ids.include?(class_id(clazz))
          append_class_entry(clazz, xml)
          @class_ids << class_id(clazz)
          
          clazz.method_list.each do |mthd|
            unless @method_ids.include?(method_id(mthd))
              append_method_entry(mthd, xml)
              @method_ids << method_id(mthd)
            end
          end
        end
      end
    end
  end
  
  xml_file = 'Dictionary.xml'
  
  File.open(File.join(Pathname.pwd, xml_file), 'w') { |f| f << builder.to_xml }
  
  dict_src_path = File.join(Pathname.pwd, xml_file)
  
  css_path = File.join(@template_dir, 'Dictionary.css')
  File.open(File.join(Pathname.pwd, 'Dictionary.plist'), 'w') { |f| f.write render_plist(@options.dictionary_name, bundle_identifier) }
  
  plist_path = File.join(Pathname.pwd, 'Dictionary.plist')
  
  dict_build_tool = File.join((@options.kit_path || '/Developer/Extras/Dictionary Development Kit'), 'bin', 'build_dict.sh')
  
  %x{"#{dict_build_tool}" "#{@options.dictionary_name}" #{dict_src_path} #{css_path} #{plist_path}}
end