@@ -15,6 +15,30 @@ def initialize(store, options)
1515 @template_dir = Pathname . new ( aliki_template_dir )
1616 end
1717
18+ ##
19+ # Generate documentation. Overrides Darkfish to use Aliki's own search index
20+ # instead of the JsonIndex generator.
21+
22+ def generate
23+ setup
24+
25+ write_style_sheet
26+ generate_index
27+ generate_class_files
28+ generate_file_files
29+ generate_table_of_contents
30+ write_search_index
31+
32+ copy_static
33+
34+ rescue => e
35+ debug_msg "%s: %s\n %s" % [
36+ e . class . name , e . message , e . backtrace . join ( "\n " )
37+ ]
38+
39+ raise
40+ end
41+
1842 ##
1943 # Copy only the static assets required by the Aliki theme. Unlike Darkfish we
2044 # don't ship embedded fonts or image sprites, so limit the asset list to keep
@@ -39,4 +63,104 @@ def write_style_sheet
3963 install_rdoc_static_file @template_dir + path , dst , options
4064 end
4165 end
66+
67+ ##
68+ # Build a search index array for Aliki's searcher.
69+
70+ def build_search_index
71+ setup
72+
73+ index = [ ]
74+
75+ @classes . each do |klass |
76+ next unless klass . display?
77+
78+ index << build_class_module_entry ( klass )
79+
80+ klass . constants . each do |const |
81+ next unless const . display?
82+
83+ index << build_constant_entry ( const , klass )
84+ end
85+ end
86+
87+ @methods . each do |method |
88+ next unless method . display?
89+
90+ index << build_method_entry ( method )
91+ end
92+
93+ index
94+ end
95+
96+ ##
97+ # Write the search index as a JavaScript file
98+ # Format: var search_data = { index: [...] }
99+ #
100+ # We still write to a .js instead of a .json because loading a JSON file triggers CORS check in browsers.
101+ # And if we simply inspect the generated pages using file://, which is often the case due to lack of the server mode,
102+ # the JSON file will be blocked by the browser.
103+
104+ def write_search_index
105+ debug_msg "Writing Aliki search index"
106+
107+ index = build_search_index
108+
109+ FileUtils . mkdir_p 'js' unless @dry_run
110+
111+ search_index_path = 'js/search_data.js'
112+ return if @dry_run
113+
114+ data = { index : index }
115+ File . write search_index_path , "var search_data = #{ JSON . generate ( data ) } ;"
116+ end
117+
118+ private
119+
120+ def build_class_module_entry ( klass )
121+ type = case klass
122+ when RDoc ::NormalClass then 'class'
123+ when RDoc ::NormalModule then 'module'
124+ else 'class'
125+ end
126+
127+ entry = {
128+ name : klass . name ,
129+ full_name : klass . full_name ,
130+ type : type ,
131+ path : klass . path
132+ }
133+
134+ snippet = klass . search_snippet
135+ entry [ :snippet ] = snippet unless snippet . empty?
136+ entry
137+ end
138+
139+ def build_method_entry ( method )
140+ type = method . singleton ? 'class_method' : 'instance_method'
141+
142+ entry = {
143+ name : method . name ,
144+ full_name : method . full_name ,
145+ type : type ,
146+ path : method . path
147+ }
148+
149+ snippet = method . search_snippet
150+ entry [ :snippet ] = snippet unless snippet . empty?
151+ entry
152+ end
153+
154+ def build_constant_entry ( const , parent )
155+ entry = {
156+ name : const . name ,
157+ full_name : "#{ parent . full_name } ::#{ const . name } " ,
158+ type : 'constant' ,
159+ path : parent . path
160+ }
161+
162+ snippet = const . search_snippet
163+ entry [ :snippet ] = snippet unless snippet . empty?
164+ entry
165+ end
42166end
0 commit comments