Class: Rhales::RueDocument
- Inherits:
-
Object
- Object
- Rhales::RueDocument
- Defined in:
- lib/rhales/rue_document.rb
Overview
High-level interface for parsed .rue files
This class provides a convenient interface to .rue files parsed by RueFormatParser. It uses RueFormatParser internally for low-level parsing and provides high-level methods for accessing sections, attributes, and extracted data.
Features:
-
High-level interface to RueFormatParser AST
-
Accurate error reporting with line/column information
-
Convenient section access methods
-
Section validation and attribute extraction
-
Variable and partial dependency analysis
-
AST-to-string conversion when needed
Note: This class represents a parsed .rue file document, similar to how HTML::Document represents a parsed HTML document.
Usage:
document = RueDocument.new(rue_content)
document.parse!
template_section = document.section('template')
variables = document.template_variables
Defined Under Namespace
Classes: InvalidSyntaxError, ParseError, SectionDuplicateError, SectionMissingError
Constant Summary collapse
- REQUIRES_ONE_OF_SECTIONS =
At least one of these sections must be present
%w[data template].freeze
- KNOWN_SECTIONS =
%w[data template logic].freeze
- ALL_SECTIONS =
KNOWN_SECTIONS.freeze
- KNOWN_DATA_ATTRIBUTES =
Known data section attributes
%w[window merge layout].freeze
Instance Attribute Summary collapse
-
#ast ⇒ Object
readonly
Returns the value of attribute ast.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#grammar ⇒ Object
readonly
Returns the value of attribute grammar.
Class Method Summary collapse
Instance Method Summary collapse
- #all_variables ⇒ Object
- #convert_node_to_string(node) ⇒ Object
- #convert_nodes_to_string(nodes) ⇒ Object
- #data_attributes ⇒ Object
- #data_variables ⇒ Object
-
#initialize(content, file_path = nil) ⇒ RueDocument
constructor
A new instance of RueDocument.
- #layout ⇒ Object
- #merge_strategy ⇒ Object
- #parse! ⇒ Object
- #partials ⇒ Object
- #schema_path ⇒ Object
- #section(name) ⇒ Object
- #section?(name) ⇒ Boolean
-
#section_node(name) ⇒ Object
Get the raw section node with location information.
- #sections ⇒ Object
- #template_variables ⇒ Object
- #window_attribute ⇒ Object
Constructor Details
#initialize(content, file_path = nil) ⇒ RueDocument
Returns a new instance of RueDocument.
44 45 46 47 48 49 |
# File 'lib/rhales/rue_document.rb', line 44 def initialize(content, file_path = nil) @content = content @file_path = file_path @grammar = RueFormatParser.new(content, file_path) @ast = nil end |
Instance Attribute Details
#ast ⇒ Object (readonly)
Returns the value of attribute ast.
42 43 44 |
# File 'lib/rhales/rue_document.rb', line 42 def ast @ast end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
42 43 44 |
# File 'lib/rhales/rue_document.rb', line 42 def content @content end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
42 43 44 |
# File 'lib/rhales/rue_document.rb', line 42 def file_path @file_path end |
#grammar ⇒ Object (readonly)
Returns the value of attribute grammar.
42 43 44 |
# File 'lib/rhales/rue_document.rb', line 42 def grammar @grammar end |
Class Method Details
.parse_file(file_path) ⇒ Object
292 293 294 295 296 297 |
# File 'lib/rhales/rue_document.rb', line 292 def parse_file(file_path) raise ArgumentError, 'Not a .rue file' unless rue_file?(file_path) file_content = File.read(file_path) new(file_content, file_path).parse! end |
.rue_file?(file_path) ⇒ Boolean
299 300 301 |
# File 'lib/rhales/rue_document.rb', line 299 def rue_file?(file_path) File.extname(file_path) == '.rue' end |
Instance Method Details
#all_variables ⇒ Object
160 161 162 |
# File 'lib/rhales/rue_document.rb', line 160 def all_variables (template_variables + data_variables).uniq end |
#convert_node_to_string(node) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rhales/rue_document.rb', line 72 def convert_node_to_string(node) case node.type when :text node.value when :variable_expression name = node.value[:name] raw = node.value[:raw] raw ? "{{{#{name}}}}" : "{{#{name}}}" when :partial_expression "{{> #{node.value[:name]}}}" when :if_block condition = node.value[:condition] if_content = convert_nodes_to_string(node.value[:if_content]) else_content = convert_nodes_to_string(node.value[:else_content]) if else_content.empty? "{{#if #{condition}}}#{if_content}{{/if}}" else "{{#if #{condition}}}#{if_content}{{else}}#{else_content}{{/if}}" end when :unless_block condition = node.value[:condition] content = convert_nodes_to_string(node.value[:content]) "{{#unless #{condition}}}#{content}{{/unless}}" when :each_block items = node.value[:items] content = convert_nodes_to_string(node.value[:content]) "{{#each #{items}}}#{content}{{/each}}" when :handlebars_expression # Handle legacy format for data sections if node.value[:raw] "{{{#{node.value[:content]}}}" else "{{#{node.value[:content]}}}" end else '' end end |
#convert_nodes_to_string(nodes) ⇒ Object
68 69 70 |
# File 'lib/rhales/rue_document.rb', line 68 def convert_nodes_to_string(nodes) nodes.map { |node| convert_node_to_string(node) }.join end |
#data_attributes ⇒ Object
115 116 117 |
# File 'lib/rhales/rue_document.rb', line 115 def data_attributes @data_attributes ||= {} end |
#data_variables ⇒ Object
156 157 158 |
# File 'lib/rhales/rue_document.rb', line 156 def data_variables extract_variables_from_section('data') end |
#layout ⇒ Object
131 132 133 |
# File 'lib/rhales/rue_document.rb', line 131 def layout data_attributes['layout'] end |
#merge_strategy ⇒ Object
127 128 129 |
# File 'lib/rhales/rue_document.rb', line 127 def merge_strategy data_attributes['merge'] end |
#parse! ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/rhales/rue_document.rb', line 51 def parse! @grammar.parse! @ast = @grammar.ast parse_data_attributes! self rescue RueFormatParser::ParseError => ex raise ParseError, "Parser error: #{ex.}" end |
#partials ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/rhales/rue_document.rb', line 144 def partials return [] unless @ast partials = [] extract_partials_from_node(@ast, partials) partials.uniq end |
#schema_path ⇒ Object
123 124 125 |
# File 'lib/rhales/rue_document.rb', line 123 def schema_path data_attributes['schema'] end |
#section(name) ⇒ Object
111 112 113 |
# File 'lib/rhales/rue_document.rb', line 111 def section(name) sections[name] end |
#section?(name) ⇒ Boolean
135 136 137 |
# File 'lib/rhales/rue_document.rb', line 135 def section?(name) @grammar.sections.key?(name) end |
#section_node(name) ⇒ Object
Get the raw section node with location information
140 141 142 |
# File 'lib/rhales/rue_document.rb', line 140 def section_node(name) @grammar.sections[name] end |
#sections ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/rhales/rue_document.rb', line 60 def sections return {} unless @ast @grammar.sections.transform_values do |section_node| convert_nodes_to_string(section_node.value[:content]) end end |
#template_variables ⇒ Object
152 153 154 |
# File 'lib/rhales/rue_document.rb', line 152 def template_variables extract_variables_from_section('template', exclude_partials: true) end |
#window_attribute ⇒ Object
119 120 121 |
# File 'lib/rhales/rue_document.rb', line 119 def window_attribute data_attributes['window'] || 'data' end |