Class: HamlLint::Document
- Inherits:
-
Object
- Object
- HamlLint::Document
- Defined in:
- lib/haml_lint/document.rb
Overview
Represents a parsed Haml document and its associated metadata.
Constant Summary collapse
- STRING_SOURCE =
File name given to source code parsed from just a string.
'(string)'
Instance Attribute Summary collapse
-
#config ⇒ HamlLint::Configuration
readonly
Configuration used to parse template.
-
#file ⇒ String
readonly
Haml template file path.
-
#indentation ⇒ String
readonly
The indentation used in the file.
-
#source ⇒ String
readonly
Original source code.
-
#source_lines ⇒ Array<String>
readonly
Original source code as an array of lines.
-
#source_was_changed ⇒ Boolean
readonly
True if the source was changed (by autocorrect).
-
#tree ⇒ HamlLint::Tree::Node
readonly
Root of the parse tree.
-
#unescape_interpolation_to_original_cache ⇒ Object
readonly
Returns the value of attribute unescape_interpolation_to_original_cache.
Instance Method Summary collapse
-
#change_source(new_source) ⇒ Object
Reparses the new source and remember that the document was changed Used when auto-correct does changes to the file.
-
#initialize(source, options) ⇒ Document
constructor
Parses the specified Haml code into a Document.
-
#last_non_empty_line ⇒ Integer
Returns the last non empty line of the document or 1 if all lines are empty.
- #write_to_disk! ⇒ Object
Constructor Details
#initialize(source, options) ⇒ Document
Parses the specified Haml code into a HamlLint::Document.
40 41 42 43 44 45 |
# File 'lib/haml_lint/document.rb', line 40 def initialize(source, ) @config = [:config] @file = .fetch(:file, STRING_SOURCE) @source_was_changed = false process_source(source) end |
Instance Attribute Details
#config ⇒ HamlLint::Configuration (readonly)
Returns Configuration used to parse template.
12 13 14 |
# File 'lib/haml_lint/document.rb', line 12 def config @config end |
#file ⇒ String (readonly)
Returns Haml template file path.
15 16 17 |
# File 'lib/haml_lint/document.rb', line 15 def file @file end |
#indentation ⇒ String (readonly)
Returns the indentation used in the file.
30 31 32 |
# File 'lib/haml_lint/document.rb', line 30 def indentation @indentation end |
#source ⇒ String (readonly)
Returns original source code.
21 22 23 |
# File 'lib/haml_lint/document.rb', line 21 def source @source end |
#source_lines ⇒ Array<String> (readonly)
Returns original source code as an array of lines.
24 25 26 |
# File 'lib/haml_lint/document.rb', line 24 def source_lines @source_lines end |
#source_was_changed ⇒ Boolean (readonly)
Returns true if the source was changed (by autocorrect).
27 28 29 |
# File 'lib/haml_lint/document.rb', line 27 def source_was_changed @source_was_changed end |
#tree ⇒ HamlLint::Tree::Node (readonly)
Returns Root of the parse tree.
18 19 20 |
# File 'lib/haml_lint/document.rb', line 18 def tree @tree end |
#unescape_interpolation_to_original_cache ⇒ Object (readonly)
Returns the value of attribute unescape_interpolation_to_original_cache.
32 33 34 |
# File 'lib/haml_lint/document.rb', line 32 def unescape_interpolation_to_original_cache @unescape_interpolation_to_original_cache end |
Instance Method Details
#change_source(new_source) ⇒ Object
Reparses the new source and remember that the document was changed Used when auto-correct does changes to the file. If the source hasn’t changed, then the document will not be marked as changed.
If the new_source fails to parse, automatically reparses the previous source to bring the document back to how it should be before re-raising the parse exception
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/haml_lint/document.rb', line 63 def change_source(new_source) return if new_source == @source check_new_source_compatible(new_source) old_source = @source begin process_source(new_source) @source_was_changed = true rescue HamlLint::Exceptions::ParseError # Reprocess the previous_source so that other linters can work on this document # object from a clean slate process_source(old_source) raise end nil end |
#last_non_empty_line ⇒ Integer
Returns the last non empty line of the document or 1 if all lines are empty
50 51 52 53 |
# File 'lib/haml_lint/document.rb', line 50 def last_non_empty_line index = source_lines.rindex { |l| !l.empty? } (index || 0) + 1 end |
#write_to_disk! ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/haml_lint/document.rb', line 80 def write_to_disk! return unless @source_was_changed if file == STRING_SOURCE raise HamlLint::Exceptions::InvalidFilePath, 'Cannot write without :file option' end File.write(file, unstrip_frontmatter(source)) @source_was_changed = false end |