|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'nokogiri' |
| 4 | +require 'digest' |
| 5 | + |
| 6 | +module Jekyll |
| 7 | + module Algolia |
| 8 | + module Hooks |
| 9 | + DOXYGEN_HTML_PATH = '_doxygen_html/main/classprecice_1_1Participant.html' |
| 10 | + DOXYGEN_URL = '/doxygen/main/classprecice_1_1Participant.html' |
| 11 | + |
| 12 | + def self.before_indexing_all(records, _context) |
| 13 | + unless File.exist?(DOXYGEN_HTML_PATH) |
| 14 | + Jekyll.logger.warn 'Algolia:', "Doxygen HTML not found at #{DOXYGEN_HTML_PATH}, skipping Doxygen indexing" |
| 15 | + return records |
| 16 | + end |
| 17 | + |
| 18 | + Jekyll.logger.info 'Algolia:', 'Parsing Doxygen Participant page for indexing...' |
| 19 | + doxygen_records = parse_doxygen_participant |
| 20 | + Jekyll.logger.info 'Algolia:', "Added #{doxygen_records.length} Doxygen records" |
| 21 | + |
| 22 | + records + doxygen_records |
| 23 | + end |
| 24 | + |
| 25 | + def self.parse_doxygen_participant |
| 26 | + html = File.read(DOXYGEN_HTML_PATH) |
| 27 | + doc = Nokogiri::HTML(html) |
| 28 | + records = [] |
| 29 | + |
| 30 | + # Extract class overview record |
| 31 | + brief = doc.at_css('.contents .textblock') |
| 32 | + brief_text = brief ? brief.text.strip : 'Main class of the preCICE API for coupling simulations.' |
| 33 | + |
| 34 | + records << build_record( |
| 35 | + title: 'Participant Class Reference', |
| 36 | + anchor: nil, |
| 37 | + html: brief_text, |
| 38 | + headings: %w[precice Participant], |
| 39 | + object_id: 'doxygen-participant-overview' |
| 40 | + ) |
| 41 | + |
| 42 | + # Extract public method records from member documentation |
| 43 | + doc.css('.contents .memitem').each do |memitem| |
| 44 | + # The anchor is on the preceding h2 > a element, or in the memname |
| 45 | + anchor_el = memitem.previous_element |
| 46 | + anchor = nil |
| 47 | + |
| 48 | + # Doxygen wraps each method doc in a div.memitem preceded by an h2.memtitle |
| 49 | + # with an anchor link. Walk back to find the anchor. |
| 50 | + while anchor_el |
| 51 | + if anchor_el.name == 'a' && anchor_el['id'] |
| 52 | + anchor = anchor_el['id'] |
| 53 | + break |
| 54 | + end |
| 55 | + # Check for anchor inside the element |
| 56 | + a_tag = anchor_el.at_css('a[id]') |
| 57 | + if a_tag |
| 58 | + anchor = a_tag['id'] |
| 59 | + break |
| 60 | + end |
| 61 | + anchor_el = anchor_el.previous_element |
| 62 | + end |
| 63 | + |
| 64 | + # Extract method name from memname |
| 65 | + memname_el = memitem.at_css('.memname') |
| 66 | + next unless memname_el |
| 67 | + |
| 68 | + method_name = extract_method_name(memname_el.text.strip) |
| 69 | + next if method_name.nil? || method_name.empty? |
| 70 | + |
| 71 | + # Extract brief description |
| 72 | + memdoc = memitem.at_css('.memdoc') |
| 73 | + description = memdoc ? memdoc.text.strip.gsub(/\s+/, ' ')[0, 500] : '' |
| 74 | + next if description.empty? |
| 75 | + |
| 76 | + records << build_record( |
| 77 | + title: "Participant::#{method_name}", |
| 78 | + anchor: anchor, |
| 79 | + html: description, |
| 80 | + headings: %w[precice Participant], |
| 81 | + object_id: "doxygen-participant-#{anchor || Digest::MD5.hexdigest(method_name)}" |
| 82 | + ) |
| 83 | + end |
| 84 | + |
| 85 | + records |
| 86 | + end |
| 87 | + |
| 88 | + def self.extract_method_name(memname_text) |
| 89 | + # memname_text looks like "void precice::Participant::initialize ()" |
| 90 | + # or "precice::Participant::Participant (...)" |
| 91 | + # Extract the last component before the parentheses |
| 92 | + match = memname_text.match(/(\w+)\s*\(/) |
| 93 | + match ? match[1] : nil |
| 94 | + end |
| 95 | + |
| 96 | + def self.build_record(title:, anchor:, html:, headings:, object_id:) |
| 97 | + { |
| 98 | + title: title, |
| 99 | + url: DOXYGEN_URL, |
| 100 | + anchor: anchor, |
| 101 | + html: html, |
| 102 | + headings: headings, |
| 103 | + type: 'content', |
| 104 | + custom_ranking: { position: 0, heading: 90 }, |
| 105 | + objectID: object_id |
| 106 | + } |
| 107 | + end |
| 108 | + |
| 109 | + private_class_method :parse_doxygen_participant, :extract_method_name, :build_record |
| 110 | + end |
| 111 | + end |
| 112 | +end |
0 commit comments