Class: PrmGenerator::FileIncluder

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/udb/prm_generator.rb

Overview

Utility class for including and processing AsciiDoc files

Class Method Summary collapse

Class Method Details

.include_file(file_path, heading_adjustment = 0, strip_title = false) ⇒ Object



726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/udb/prm_generator.rb', line 726

def self.include_file(file_path, heading_adjustment = 0, strip_title = false)
  return "_Documentation not available_" unless File.exist?(file_path)

  begin
    content = File.read(file_path, encoding: 'UTF-8')
    content = ContentSanitizer.sanitize(content)

    # Strip the first heading if requested (for auto-generated content)
    if strip_title
      content = content.gsub(/^=+\s+.*?\n/, '')
    end

    # Resolve relative includes
    source_dir = File.dirname(file_path)
    content = content.gsub(/^include::([^\/][^\[\]]*\.edn)\[\]$/) do
      relative_path = $1
      absolute_path = File.join(source_dir, relative_path)

      if File.exist?(absolute_path)
        "include::#{absolute_path}[]"
      else
        "include::#{relative_path}[]"  # Let AsciiDoctor handle the error
      end
    end

    # Adjust heading levels using shared utility
    if heading_adjustment != 0
      content = PrmGenerator.adjust_heading_levels(content, heading_adjustment)
    end

    content
  rescue StandardError => e
    puts "[WARN] Failed to include file #{file_path}: #{e.message}"
    "_Error loading documentation: #{e.message}_"
  end
end