Class: Asciidoctor::IncludeExt::TagLinesSelector
- Inherits:
-
Object
- Object
- Asciidoctor::IncludeExt::TagLinesSelector
- Defined in:
- lib/asciidoctor/include_ext/tag_lines_selector.rb
Overview
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.
Instance Attribute Summary collapse
-
#first_included_lineno ⇒ Integer?
readonly
1-based line number of the first included line, or ‘nil` if none.
Class Method Summary collapse
-
.handles?(_, attributes) ⇒ Boolean
‘true` if the attributes hash contains a key `“tag”` or `“tags”`.
Instance Method Summary collapse
-
#include?(line, line_num) ⇒ Boolean
Returns ‘true` if the given line should be included, `false` otherwise.
-
#initialize(target, attributes, logger: Logging.default_logger) ⇒ TagLinesSelector
constructor
A new instance of TagLinesSelector.
-
#to_proc ⇒ Proc
#include? method as a Proc.
Constructor Details
#initialize(target, attributes, logger: Logging.default_logger) ⇒ TagLinesSelector
Returns a new instance of TagLinesSelector.
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 = ::Set.new end |
Instance Attribute Details
#first_included_lineno ⇒ Integer? (readonly)
Returns 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”`.
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
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.
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_proc ⇒ Proc
Returns #include? method as a Proc.
100 101 102 |
# File 'lib/asciidoctor/include_ext/tag_lines_selector.rb', line 100 def to_proc method(:include?).to_proc end |