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.
-
#write_to_stdout ⇒ Boolean
readonly
True if source changes (from autocorrect) should be written to stdout instead of disk.
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.
44 45 46 47 48 49 50 |
# File 'lib/haml_lint/document.rb', line 44 def initialize(source, ) @config = [:config] @file = .fetch(:file, STRING_SOURCE) @write_to_stdout = [:write_to_stdout] @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.
33 34 35 |
# File 'lib/haml_lint/document.rb', line 33 def indentation @indentation end |
#source ⇒ String (readonly)
Returns original source code.
24 25 26 |
# File 'lib/haml_lint/document.rb', line 24 def source @source end |
#source_lines ⇒ Array<String> (readonly)
Returns original source code as an array of lines.
27 28 29 |
# File 'lib/haml_lint/document.rb', line 27 def source_lines @source_lines end |
#source_was_changed ⇒ Boolean (readonly)
Returns true if the source was changed (by autocorrect).
30 31 32 |
# File 'lib/haml_lint/document.rb', line 30 def source_was_changed @source_was_changed end |
#tree ⇒ HamlLint::Tree::Node (readonly)
Returns Root of the parse tree.
21 22 23 |
# File 'lib/haml_lint/document.rb', line 21 def tree @tree end |
#unescape_interpolation_to_original_cache ⇒ Object (readonly)
Returns the value of attribute unescape_interpolation_to_original_cache.
35 36 37 |
# File 'lib/haml_lint/document.rb', line 35 def unescape_interpolation_to_original_cache @unescape_interpolation_to_original_cache end |
#write_to_stdout ⇒ Boolean (readonly)
Returns true if source changes (from autocorrect) should be written to stdout instead of disk.
18 19 20 |
# File 'lib/haml_lint/document.rb', line 18 def write_to_stdout @write_to_stdout 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/haml_lint/document.rb', line 68 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
55 56 57 58 |
# File 'lib/haml_lint/document.rb', line 55 def last_non_empty_line index = source_lines.rindex { |l| !l.empty? } (index || 0) + 1 end |
#write_to_disk! ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/haml_lint/document.rb', line 85 def write_to_disk! return unless @source_was_changed if file == STRING_SOURCE raise HamlLint::Exceptions::InvalidFilePath, 'Cannot write without :file option' end if @write_to_stdout $stdout << unstrip_frontmatter(source) else File.write(file, unstrip_frontmatter(source)) end @source_was_changed = false end |