|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'json' |
| 4 | + |
| 5 | +include T('default/fulldoc/html') |
| 6 | + |
| 7 | +def init |
| 8 | + options.objects = objects = run_verifier(options.objects) |
| 9 | + |
| 10 | + return serialize_onefile if options.onefile |
| 11 | + |
| 12 | + generate_assets |
| 13 | + serialize('_index.html') |
| 14 | + options.files.each_with_index do |file, _i| |
| 15 | + serialize_file(file, file.title) |
| 16 | + end |
| 17 | + |
| 18 | + options.delete(:objects) |
| 19 | + |
| 20 | + objects.each do |object| |
| 21 | + begin |
| 22 | + serialize(object) |
| 23 | + rescue StandardError => e |
| 24 | + path = options.serializer.serialized_path(object) |
| 25 | + log.error "Exception occurred while generating '#{path}'" |
| 26 | + log.backtrace(e) |
| 27 | + end |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +def generate_assets |
| 32 | + layout_template = Templates::Engine.template(:aliki, :layout, :html) |
| 33 | + layout = Object.new.extend(layout_template) |
| 34 | + (layout.javascripts + layout.stylesheets).uniq.each do |file| |
| 35 | + next if file == 'js/search_data.js' |
| 36 | + |
| 37 | + asset(file, File.read(layout_template.find_file(file))) |
| 38 | + end |
| 39 | + |
| 40 | + asset('js/search_data.js', aliki_search_data) |
| 41 | +end |
| 42 | + |
| 43 | +def aliki_search_data |
| 44 | + entries = [] |
| 45 | + searchable_objects.each do |object| |
| 46 | + entries << aliki_search_entry(object) |
| 47 | + end |
| 48 | + |
| 49 | + "var search_data = #{JSON.generate(:index => entries.compact)};" |
| 50 | +end |
| 51 | + |
| 52 | +def searchable_objects |
| 53 | + objects = Registry.all(:class, :module, :method, :constant, :classvariable) |
| 54 | + run_verifier(objects).reject do |object| |
| 55 | + object.respond_to?(:root?) && object.root? |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | +def aliki_search_entry(object) |
| 60 | + path = url_for(object, nil, false) |
| 61 | + return unless path |
| 62 | + |
| 63 | + { |
| 64 | + :name => aliki_search_name(object), |
| 65 | + :full_name => object.path, |
| 66 | + :type => aliki_search_type(object), |
| 67 | + :path => path |
| 68 | + }.tap do |entry| |
| 69 | + snippet = aliki_search_snippet(object) |
| 70 | + entry[:snippet] = snippet unless snippet.empty? |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +def aliki_search_name(object) |
| 75 | + if object.respond_to?(:name) |
| 76 | + object.name.to_s |
| 77 | + else |
| 78 | + object.path.to_s |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +def aliki_search_type(object) |
| 83 | + case object |
| 84 | + when CodeObjects::ClassObject |
| 85 | + 'class' |
| 86 | + when CodeObjects::ModuleObject |
| 87 | + 'module' |
| 88 | + when CodeObjects::MethodObject |
| 89 | + object.scope == :class ? 'class_method' : 'instance_method' |
| 90 | + when CodeObjects::ConstantObject, CodeObjects::ClassVariableObject |
| 91 | + 'constant' |
| 92 | + else |
| 93 | + object.type.to_s |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +def aliki_search_snippet(object) |
| 98 | + return '' unless object.respond_to?(:docstring) |
| 99 | + |
| 100 | + summary = object.docstring.summary.to_s.strip.gsub(/\s+/, ' ') |
| 101 | + h(summary) |
| 102 | +end |
0 commit comments