private void filterForTags(Document document, PreprocessorReader reader, String target,
                                   Path includee, String includeeText, Map<String, Object> attributes) {
          Map<String, Boolean> tags = determineTagsToFilterFor(attributes);

          // if no tags are configured, include the whole file if it has any content
          if (tags.isEmpty()) {
            if (!includeeText.isEmpty()) {
              pushMarkedInclude(document, reader, target, new StringBuilder(includeeText), 1, includee, attributes);
            }
            return;
          }

          // determine the base select value when the tag stack is empty
          // and the wildcard from ** and * tags
          boolean baseSelect;
          Boolean[] wildcard = {null};
          if (tags.containsKey("**")) {
            baseSelect = tags.remove("**");
            if (tags.containsKey("*")) {
              wildcard[0] = tags.remove("*");
            } else if (!baseSelect && !tags.values().iterator().next()) {
              wildcard[0] = true;
            }
          } else if (tags.containsKey("*")) {
            wildcard[0] = tags.remove("*");
            if ("*".equals(tags.keySet().iterator().next())) {
              baseSelect = !wildcard[0];
            } else {
              baseSelect = false;
            }
          } else {
            baseSelect = !tags.containsValue(true);
          }
          boolean[] select = {baseSelect};

          StringBuilder includedText = new StringBuilder();
          record TagStackEntry(String tag, boolean select, int lineNumber) {
          }
          Deque<TagStackEntry> tagStack = new ArrayDeque<>();
          Set<String> selectedTags = new HashSet<>();
          String[] activeTag = {null};
          int[] firstLine = {0};
          int[] lineNumber = {0};

          includeeText.lines().forEachOrdered(line -> {
            lineNumber[0]++;
            Matcher tagLineMatcher = TAG_LINE_PATTERN.matcher(line);
            if (tagLineMatcher.find()) {
              // if we look at a tag start or end line, never include the line
              // but update the tag stack, active tag, and select state
              String thisTag = tagLineMatcher.group("tag");
              if (tagLineMatcher.group("boundary").equals("end")) {
                // if we look at an end tag ...
                if (thisTag.equals(activeTag[0])) {
                  // ... for the currently active tag
                  tagStack.pop();
                  if (tagStack.isEmpty()) {
                    // update the state from the default values
                    activeTag[0] = null;
                    select[0] = baseSelect;
                  } else {
                    // or update the state from the tag stack head
                    TagStackEntry tagStackHead = tagStack.peek();
                    activeTag[0] = tagStackHead.tag();
                    select[0] = tagStackHead.select();
                  }
                } else if (tags.containsKey(thisTag)) {
                  // ... for a different tag on which is filtered too
                  // log a warning about mismatched or unexpected tag end
                  // if the tag in question is not filtered on,
                  // misplaced or interleaved tags are not considered a problem
                  tagStack
                    .stream()
                    .filter(tagStackEntry -> tagStackEntry.tag().equals(thisTag))
                    .findFirst()
                    .ifPresentOrElse(tagStackEntry -> {
                        tagStack.remove(tagStackEntry);
                        log(new LogRecord(WARN, format(
                          "mismatched end tag (expected '%s' but found '%s') at line %d of include file: %s",
                          activeTag[0], thisTag, lineNumber[0], target)));
                      }, () ->
                        log(new LogRecord(WARN, format(
                          "unexpected end tag '%s' at line %d of include file: %s",
                          thisTag, lineNumber[0], target)))
                    );
                }
              } else if (tags.containsKey(thisTag)) {
                // if we look at a start tag that is filtered for
                // update the selected tags, tag stack, active tag, and select state
                select[0] = tags.get(thisTag);
                if (select[0]) {
                  selectedTags.add(thisTag);
                }
                activeTag[0] = thisTag;
                tagStack.push(new TagStackEntry(thisTag, select[0], lineNumber[0]));
              } else if (wildcard[0] != null) {
                // if we look at a start tag that is not filtered for,
                // but we have an explicit or implicit one-star wildcard
                // update the tag stack, active tag, and select state
                select[0] = ((activeTag[0] == null) || select[0]) && wildcard[0];
                activeTag[0] = thisTag;
                tagStack.push(new TagStackEntry(thisTag, select[0], lineNumber[0]));
              }
            } else if (select[0]) {
              // if we do not look at a tag start or end line
              // and current select state is true, include the line
              if (firstLine[0] == 0) {
                // record the first included line number
                firstLine[0] = lineNumber[0];
              }
              includedText.append(line).append('\n');
            }
          });

          // if after all lines are iterated there are still entries on the stack,
          // we found a start tag that was not closed properly, so complain
          if (!tagStack.isEmpty()) {
            tagStack.forEach(tagStackEntry ->
              log(new LogRecord(WARN, format(
                "detected unclosed tag '%s' starting at line %d of include file: %s",
                tagStackEntry.tag(), tagStackEntry.lineNumber(), target))));
          }

          // if there are tags we wanted to include but that were not selected, complain
          tags.entrySet().removeIf(entry -> !entry.getValue() || selectedTags.contains(entry.getKey()));
          if (!tags.isEmpty()) {
            log(new LogRecord(WARN, format(
              "tag%s '%s' not found in include file: %s",
              tags.size() > 1 ? "s" : "", join(", ", tags.keySet()), target)));
          }

          // include matched lines, if there were any in the configured tags specification
          if (firstLine[0] != 0) {
            pushMarkedInclude(document, reader, target, includedText, firstLine[0], includee, attributes);
          }
        }
Filter the includee's text for the specified tags and push the result.

@param document The currently rendered document
@param reader The current reader
@param target The target of the include directive, i.e. the included file starting with {@code ../}
@param includee The resolved includee
@param includeeText The text of the includee
@param attributes The attributes used on the include directive
build-logic/asciidoc-extensions/src/main/java/org/spockframework/plugins/asciidoctor/IncludedSourceLinker.java