Class: Asciidoctor::IncludeExt::LinenoLinesSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/include_ext/lineno_lines_selector.rb

Overview

Note:

Instance of this class can be used only once, as a predicate to filter a single include directive.

Lines selector that selects lines of the content to be included based on the specified ranges of line numbers.

Examples:

include::some-file.adoc[lines=1;3..4;6..-1]
selector = LinenoLinesSelector.new("some-file.adoc", {"lines" => "1;3..4;6..-1"})
IO.foreach(filename).select.with_index(1, &selector)

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_, attributes) ⇒ LinenoLinesSelector

Returns a new instance of LinenoLinesSelector.

Parameters:

  • attributes (Hash<String, String>)

    the attributes parsed from the `include::[]`s attributes slot. It must contain a key `“lines”`.



34
35
36
37
# File 'lib/asciidoctor/include_ext/lineno_lines_selector.rb', line 34

def initialize(_, attributes, **)
  @ranges = parse_attribute(attributes['lines'])
  @first_included_lineno = @ranges.last.first unless @ranges.empty?
end

Instance Attribute Details

#first_included_linenoInteger? (readonly)

Returns 1-based line number of the first included line, or `nil` if none.

Returns:

  • (Integer, nil)

    1-based line number of the first included line, or `nil` if none.



23
24
25
# File 'lib/asciidoctor/include_ext/lineno_lines_selector.rb', line 23

def first_included_lineno
  @first_included_lineno
end

Class Method Details

.handles?(_, attributes) ⇒ Boolean

Returns `true` if the attributes hash contains a key `“lines”`.

Parameters:

  • attributes (Hash<String, String>)

    the attributes parsed from the `include::[]`s attributes slot.

Returns:

  • (Boolean)

    `true` if the attributes hash contains a key `“lines”`.



28
29
30
# File 'lib/asciidoctor/include_ext/lineno_lines_selector.rb', line 28

def self.handles?(_, attributes)
  attributes.key? 'lines'
end

Instance Method Details

#include?(_, line_num) ⇒ Boolean

Note:

This method modifies state of this object. It's supposed to be called successively with each line of the content being included. See example.

Returns `true` if the given line should be included, `false` otherwise.

Parameters:

  • line_num (Integer)

    1-based line number.

Returns:

  • (Boolean)

    `true` to select the line, or `false` to reject.



47
48
49
50
51
52
53
# File 'lib/asciidoctor/include_ext/lineno_lines_selector.rb', line 47

def include?(_, line_num)
  return false if @ranges.empty?

  ranges = @ranges
  ranges.pop while !ranges.empty? && ranges.last.last < line_num
  ranges.last.cover?(line_num) if !ranges.empty?
end

#to_procProc

Returns #include? method as a Proc.

Returns:



56
57
58
# File 'lib/asciidoctor/include_ext/lineno_lines_selector.rb', line 56

def to_proc
  method(:include?).to_proc
end