Class: Asciidoctor::IncludeExt::TagLinesSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/include_ext/tag_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 based on the specified tags.

Examples:

include::some-file.adoc[tags=snippets;!snippet-b]
include::some-file.adoc[tag=snippets]
selector = TagLinesSelector.new("some-file.adoc", {"tag" => "snippets"})
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(target, attributes, logger: Logging.default_logger) ⇒ TagLinesSelector

Returns a new instance of TagLinesSelector.

Parameters:

  • target (String)

    name of the source file to include as specified in the target slot of the ‘include::[]` directive.

  • attributes (Hash<String, String>)

    the attributes parsed from the ‘include::[]`s attributes slot. It must contain a key `“tag”` or `“tags”`.

  • logger (Logger) (defaults to: Logging.default_logger)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/asciidoctor/include_ext/tag_lines_selector.rb', line 43

def initialize(target, attributes, logger: Logging.default_logger, **)
  tag_flags =
    if attributes.key? 'tag'
      parse_attribute(attributes['tag'], true)
    else
      parse_attribute(attributes['tags'])
    end

  wildcard = tag_flags.delete('*')
  if tag_flags.key? '**'
    default_state = tag_flags.delete('**')
    wildcard = default_state if wildcard.nil?
  else
    default_state = !tag_flags.value?(true)
  end

  # "immutable"
  @target = target
  @logger = logger
  @tag_flags = tag_flags.freeze
  @wildcard = wildcard
  @tag_directive_rx = /\b(?:tag|(end))::(\S+)\[\](?=$| )/.freeze

  # mutable (state variables)
  @stack = [[nil, default_state]]
  @state = default_state
  @used_tags = ::Set.new
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.



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

def first_included_lineno
  @first_included_lineno
end

Class Method Details

.handles?(_, attributes) ⇒ Boolean

Returns ‘true` if the attributes hash contains a key `“tag”` or `“tags”`.

Parameters:

  • attributes (Hash<String, String>)

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

Returns:

  • (Boolean)

    ‘true` if the attributes hash contains a key `“tag”` or `“tags”`.



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

def self.handles?(_, attributes)
  attributes.key?('tag') || attributes.key?('tags')
end

Instance Method Details

#include?(line, 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 (String)
  • line_num (Integer)

    1-based line number.

Returns:

  • (Boolean)

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/asciidoctor/include_ext/tag_lines_selector.rb', line 81

def include?(line, line_num)
  tag_type, tag_name = parse_tag_directive(line)

  case tag_type
  when :start
    enter_region!(tag_name, line_num)
    false
  when :end
    exit_region!(tag_name, line_num)
    false
  when nil
    if @state && @first_included_lineno.nil?
      @first_included_lineno = line_num
    end
    @state
  end
end

#to_procProc

Returns #include? method as a Proc.

Returns:



100
101
102
# File 'lib/asciidoctor/include_ext/tag_lines_selector.rb', line 100

def to_proc
  method(:include?).to_proc
end