Class: Sakusei::ErbProcessor::ErbContext
- Inherits:
-
Object
- Object
- Sakusei::ErbProcessor::ErbContext
- Defined in:
- lib/sakusei/erb_processor.rb
Overview
Context object that provides helper methods for ERB templates
Instance Method Summary collapse
-
#document_headings(path = nil) ⇒ Object
Extracts document headings as a JSON array for use with the Contents component.
-
#env(name, default = nil) ⇒ Object
Helper for reading environment variables.
-
#image_path(relative_path) ⇒ Object
Resolves a relative image path (relative to the source document) to a file:// URI that Puppeteer can load during PDF generation.
-
#include_file(path) ⇒ Object
Helper method to include file content directly.
-
#initialize(base_dir, source_file: nil) ⇒ ErbContext
constructor
A new instance of ErbContext.
-
#sh(command) ⇒ Object
Helper for executing shell commands.
-
#template_binding ⇒ Object
Returns the binding of this ErbContext instance so that ERB local variables (e.g. <% x = 1 %>) persist across the full template evaluation and helper methods (today, include_file, etc.) are callable as self.
-
#today(format = '%Y-%m-%d') ⇒ Object
Helper for current date.
Constructor Details
#initialize(base_dir, source_file: nil) ⇒ ErbContext
Returns a new instance of ErbContext.
30 31 32 33 |
# File 'lib/sakusei/erb_processor.rb', line 30 def initialize(base_dir, source_file: nil) @base_dir = base_dir @source_file = source_file end |
Instance Method Details
#document_headings(path = nil) ⇒ Object
Extracts document headings as a JSON array for use with the Contents component. Only includes headings that appear after the Contents component tag in the file. Normalises heading depth relative to h2: h2 → level 1, h3 → level 2, etc.
Usage:
<%= document_headings %> # reads current source file
<%= document_headings('./other.md') %> # reads a specific file
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sakusei/erb_processor.rb', line 80 def document_headings(path = nil) target = path ? File.(path, @base_dir) : @source_file return '[]' unless target && File.exist?(target) items = [] past_contents = false File.foreach(target) do |line| line = line.chomp past_contents = true if !past_contents && line.match?(/vue-component\s+name=["']Contents["']/) next unless past_contents m = line.match(/^(#+)\s+(.+)/) next unless m raw_level = m[1].length next if raw_level < 2 # skip h1 document title title = m[2].strip items << { title: title, level: raw_level - 1, slug: slugify(title) } end items.to_json end |
#env(name, default = nil) ⇒ Object
Helper for reading environment variables
54 55 56 |
# File 'lib/sakusei/erb_processor.rb', line 54 def env(name, default = nil) ENV.fetch(name, default) end |
#image_path(relative_path) ⇒ Object
Resolves a relative image path (relative to the source document) to a file:// URI that Puppeteer can load during PDF generation.
Usage in markdown:
<%= image_path('images/photo.jpg') %>
68 69 70 71 |
# File 'lib/sakusei/erb_processor.rb', line 68 def image_path(relative_path) full_path = File.(relative_path, @base_dir) "file://#{full_path}" end |
#include_file(path) ⇒ Object
Helper method to include file content directly
43 44 45 46 |
# File 'lib/sakusei/erb_processor.rb', line 43 def include_file(path) full_path = File.(path, @base_dir) File.exist?(full_path) ? File.read(full_path) : "<!-- File not found: #{path} -->" end |
#sh(command) ⇒ Object
Helper for executing shell commands
59 60 61 |
# File 'lib/sakusei/erb_processor.rb', line 59 def sh(command) `#{command}`.chomp end |
#template_binding ⇒ Object
Returns the binding of this ErbContext instance so that ERB local variables (e.g. <% x = 1 %>) persist across the full template evaluation and helper methods (today, include_file, etc.) are callable as self.
38 39 40 |
# File 'lib/sakusei/erb_processor.rb', line 38 def template_binding binding end |
#today(format = '%Y-%m-%d') ⇒ Object
Helper for current date
49 50 51 |
# File 'lib/sakusei/erb_processor.rb', line 49 def today(format = '%Y-%m-%d') Date.today.strftime(format) end |