Module: Legate::Web::DocumentationRoutes::Helpers
- Defined in:
- lib/legate/web/routes/documentation_routes.rb
Instance Method Summary collapse
- #generate_summary(markdown_content, max_lines = 5) ⇒ Object
-
#process_code_blocks(html) ⇒ Object
Process code blocks to add language classes and data attributes.
- #render_markdown(file_path) ⇒ Object
Instance Method Details
#generate_summary(markdown_content, max_lines = 5) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/legate/web/routes/documentation_routes.rb', line 66 def generate_summary(markdown_content, max_lines = 5) return '' if markdown_content.nil? || markdown_content.empty? lines = markdown_content.lines summary_lines = [] non_empty_lines_count = 0 lines.each do |line| stripped_line = line.strip if !stripped_line.empty? && !stripped_line.start_with?('#') # Ignore headers for summary start summary_lines << stripped_line non_empty_lines_count += 1 break if non_empty_lines_count >= max_lines elsif !summary_lines.empty? && stripped_line.empty? # Break on first blank line after content started break elsif summary_lines.empty? && stripped_line.start_with?('#') # Skip leading headers next end end summary_lines.join(' ').gsub(/\*\*|\*|_|`/, '') # Basic stripping of markdown end |
#process_code_blocks(html) ⇒ Object
Process code blocks to add language classes and data attributes
56 57 58 59 60 61 62 63 64 |
# File 'lib/legate/web/routes/documentation_routes.rb', line 56 def process_code_blocks(html) # Find fenced code blocks with language specifications html.gsub(%r{<pre><code\s+class="language-(\w+)">(.*?)</code></pre>}m) do |match| language = ::Regexp.last_match(1) code_content = ::Regexp.last_match(2) # Replace with our enhanced version that adds data-lang attribute %(<pre data-lang="#{language}"><code class="language-#{language}">#{code_content}</code></pre>) end end |
#render_markdown(file_path) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/legate/web/routes/documentation_routes.rb', line 12 def render_markdown(file_path) public_docs_pathname = Pathname.new(File.(File.join(settings.root, 'public', 'docs'))) target_file_pathname = Pathname.new(File.(file_path)) # Check if the target file is within the public_docs_pathname directory # and is an actual file that exists. # The `ascend` method iterates upwards from the file to its parent directories. # We check if any of these parent paths match our intended docs root. is_within_docs_dir = false target_file_pathname.ascend do |p| if p == public_docs_pathname is_within_docs_dir = true break end break if p.root? # Stop if we reach the filesystem root end unless is_within_docs_dir && target_file_pathname.file? && target_file_pathname.exist? logger.warn "Markdown render: Path not valid or file does not exist. Target: '#{target_file_pathname}', Expected base: '#{public_docs_pathname}'" return nil end markdown_content = File.read(target_file_pathname.to_s, encoding: 'UTF-8') # Configure Kramdown with enhanced rendering options - now using GFM html = Kramdown::Document.new( markdown_content, input: 'GFM', # GitHub Flavored Markdown syntax_highlighter: nil, # We'll apply our custom highlighting via CSS hard_wrap: false # Don't convert newlines to <br> ).to_html # Post-process HTML to add language tags to code blocks process_code_blocks(html) rescue Errno::ENOENT # Should be caught by File.exist? check now, but good fallback logger.warn "Markdown file not found (render_markdown): #{target_file_pathname}" nil rescue StandardError => e logger.error "Error rendering markdown for #{target_file_pathname}: #{e.}" logger.error e.backtrace.join("\n") # Add backtrace to help debugging nil end |