asciidoctor-include-ext-0.3.1/0000755000175000017500000000000013527456007014762 5ustar srudsrudasciidoctor-include-ext-0.3.1/asciidoctor-include-ext.gemspec0000644000175000017500000000257713527456007023064 0ustar srudsrudrequire File.expand_path('../lib/asciidoctor/include_ext/version', __FILE__) Gem::Specification.new do |s| s.name = 'asciidoctor-include-ext' s.version = Asciidoctor::IncludeExt::VERSION s.author = 'Jakub Jirutka' s.email = 'jakub@jirutka.cz' s.homepage = 'https://github.com/jirutka/asciidoctor-include-ext' s.license = 'MIT' s.summary = "Asciidoctor's standard include::[] processor reimplemented as an extension" s.description = <= 1.5.6', '< 3.0.0' s.add_development_dependency 'corefines', '~> 1.11' s.add_development_dependency 'kramdown', '~> 1.16' s.add_development_dependency 'rake', '~> 12.0' s.add_development_dependency 'rspec', '~> 3.7' s.add_development_dependency 'rubocop', '~> 0.51.0' s.add_development_dependency 'simplecov', '~> 0.15' s.add_development_dependency 'yard', '~> 0.9' end asciidoctor-include-ext-0.3.1/LICENSE0000644000175000017500000000210213527456007015762 0ustar srudsrudThe MIT License Copyright 2017 Jakub Jirutka . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. asciidoctor-include-ext-0.3.1/lib/0000755000175000017500000000000013527456007015530 5ustar srudsrudasciidoctor-include-ext-0.3.1/lib/asciidoctor-include-ext.rb0000644000175000017500000000010013527456007022566 0ustar srudsrud# frozen_string_literal: true require 'asciidoctor/include_ext' asciidoctor-include-ext-0.3.1/lib/asciidoctor/0000755000175000017500000000000013527456007020033 5ustar srudsrudasciidoctor-include-ext-0.3.1/lib/asciidoctor/include_ext.rb0000644000175000017500000000033213527456007022661 0ustar srudsrud# frozen_string_literal: true require 'asciidoctor/extensions' require 'asciidoctor/include_ext/include_processor' Asciidoctor::Extensions.register do include_processor Asciidoctor::IncludeExt::IncludeProcessor end asciidoctor-include-ext-0.3.1/lib/asciidoctor/include_ext/0000755000175000017500000000000013527456007022336 5ustar srudsrudasciidoctor-include-ext-0.3.1/lib/asciidoctor/include_ext/logging.rb0000644000175000017500000000111513527456007024307 0ustar srudsrud# frozen_string_literal: true require 'logger' require 'asciidoctor' require 'asciidoctor/include_ext/version' module Asciidoctor::IncludeExt # Helper module for getting default Logger based on the Asciidoctor version. module Logging module_function # @return [Logger] the default `Asciidoctor::Logger` if using Asciidoctor # 1.5.7 or later, or Ruby's `Logger` that outputs to `STDERR`. def default_logger if defined? ::Asciidoctor::LoggerManager ::Asciidoctor::LoggerManager.logger else ::Logger.new(STDERR) end end end end asciidoctor-include-ext-0.3.1/lib/asciidoctor/include_ext/lineno_lines_selector.rb0000644000175000017500000000534513527456007027250 0ustar srudsrud# frozen_string_literal: true require 'asciidoctor/include_ext/version' module Asciidoctor::IncludeExt # Lines selector that selects lines of the content to be included based on # the specified ranges of line numbers. # # @note Instance of this class can be used only once, as a predicate to # filter a single include directive. # # @example # include::some-file.adoc[lines=1;3..4;6..-1] # # @example # selector = LinenoLinesSelector.new("some-file.adoc", {"lines" => "1;3..4;6..-1"}) # IO.foreach(filename).select.with_index(1, &selector) # # @see http://asciidoctor.org/docs/user-manual#by-line-ranges class LinenoLinesSelector # @return [Integer, nil] 1-based line number of the first included line, # or `nil` if none. attr_reader :first_included_lineno # @param attributes [Hash] the attributes parsed from the # `include::[]`s attributes slot. # @return [Boolean] `true` if the *attributes* hash contains a key `"lines"`. def self.handles?(_, attributes) attributes.key? 'lines' end # @param attributes [Hash] the attributes parsed from the # `include::[]`s attributes slot. It must contain a key `"lines"`. def initialize(_, attributes, **) @ranges = parse_attribute(attributes['lines']) @first_included_lineno = @ranges.last.first unless @ranges.empty? end # Returns `true` if the given line should be included, `false` otherwise. # # @note This method modifies state of this object. It's supposed to be # called successively with each line of the content being included. # See {LinenoLinesSelector example}. # # @param line_num [Integer] 1-based *line* number. # @return [Boolean] `true` to select the *line*, or `false` to reject. 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 # @return [Proc] {#include?} method as a Proc. def to_proc method(:include?).to_proc end protected # @param lines_def [String] a comma or semicolon separated numbers and # and ranges (e.g. `1..2`) specifying lines to be selected, or rejected # if prefixed with "!". # @return [Array] an array of ranges sorted by the range begin in # _descending_ order. def parse_attribute(lines_def) lines_def .split(/[,;]/) .map! { |line_def| from, to = line_def.split('..', 2).map(&:to_i) to ||= from to = ::Float::INFINITY if to == -1 (from..to) }.sort! do |a, b| b.first <=> a.first end end end end asciidoctor-include-ext-0.3.1/lib/asciidoctor/include_ext/include_processor.rb0000644000175000017500000001242313527456007026407 0ustar srudsrud# frozen_string_literal: true require 'logger' require 'open-uri' require 'asciidoctor/include_ext/version' require 'asciidoctor/include_ext/reader_ext' require 'asciidoctor/include_ext/lineno_lines_selector' require 'asciidoctor/include_ext/logging' require 'asciidoctor/include_ext/tag_lines_selector' require 'asciidoctor' require 'asciidoctor/extensions' module Asciidoctor::IncludeExt # Asciidoctor preprocessor for processing `include::[]` directives # in the source document. # # @see http://asciidoctor.org/docs/user-manual/#include-directive class IncludeProcessor < ::Asciidoctor::Extensions::IncludeProcessor # @param selectors [Array] an array of selectors that can filter # specified portions of the document to include # (see ). # @param logger [Logger] the logger to use for logging warning and errors # from this object and selectors. def initialize(selectors: [LinenoLinesSelector, TagLinesSelector], logger: Logging.default_logger, **) super @selectors = selectors.dup.freeze @logger = logger end # @param reader [Asciidoctor::Reader] # @param target [String] name of the source file to include as specified # in the target slot of the `include::[]` directive. # @param attributes [Hash] parsed attributes of the # `include::[]` directive. def process(_, reader, target, attributes) unless include_allowed? target, reader reader.unshift_line("link:#{target}[]") return end if (max_depth = reader.exceeded_max_depth?) logger.error "#{reader.line_info}: maximum include depth of #{max_depth} exceeded" return end unless (path = resolve_target_path(target, reader)) if attributes.key? 'optional-option' reader.shift else logger.error "#{reader.line_info}: include target not found: #{target}" unresolved_include!(target, reader) end return end selector = lines_selector_for(target, attributes) begin lines = read_lines(path, selector) rescue => e # rubocop:disable RescueWithoutErrorClass logger.error "#{reader.line_info}: failed to read include file: #{path}: #{e}" unresolved_include!(target, reader) return end if selector && selector.respond_to?(:first_included_lineno) incl_offset = selector.first_included_lineno end unless lines.empty? reader.push_include(lines, path, target, incl_offset || 1, attributes) end end protected attr_reader :logger # @param target (see #process) # @param reader (see #process) # @return [Boolean] `true` if it's allowed to include the *target*, # `false` otherwise. def include_allowed?(target, reader) doc = reader.document return false if doc.safe >= ::Asciidoctor::SafeMode::SECURE return false if doc.attributes.fetch('max-include-depth', 64).to_i < 1 return false if target_uri?(target) && !doc.attributes.key?('allow-uri-read') true end # @param target (see #process) # @param reader (see #process) # @return [String, nil] file path or URI of the *target*, or `nil` if not found. def resolve_target_path(target, reader) return target if target_uri? target # Include file is resolved relative to dir of the current include, # or base_dir if within original docfile. path = reader.document.normalize_system_path(target, reader.dir, nil, target_name: 'include file') path if ::File.file?(path) end # Reads the specified file as individual lines, filters them using the # *selector* (if provided) and returns those lines in an array. # # @param filename [String] path of the file to be read. # @param selector [#to_proc, nil] predicate to filter lines that should be # included in the output. It must accept two arguments: line and # the line number. If `nil` is given, all lines are passed. # @return [Array] an array of read lines. def read_lines(filename, selector) if selector IO.foreach(filename).select.with_index(1, &selector) else open(filename, &:read) end end # Finds and initializes a lines selector that can handle the specified include. # # @param target (see #process) # @param attributes (see #process) # @return [#to_proc, nil] an instance of lines selector, or `nil` if not found. def lines_selector_for(target, attributes) if (klass = @selectors.find { |s| s.handles? target, attributes }) klass.new(target, attributes, logger: logger) end end # Replaces the include directive in ouput with a notice that it has not # been resolved. # # @param target (see #process) # @param reader (see #process) def unresolved_include!(target, reader) reader.unshift_line("Unresolved directive in #{reader.path} - include::#{target}[]") end private # @param target (see #process) # @return [Boolean] `true` if the *target* is an URI, `false` otherwise. def target_uri?(target) ::Asciidoctor::Helpers.uriish?(target) end end end asciidoctor-include-ext-0.3.1/lib/asciidoctor/include_ext/version.rb0000644000175000017500000000023713527456007024352 0ustar srudsrud# frozen_string_literal: true module Asciidoctor module IncludeExt # Version of the asciidoctor-include-ext gem. VERSION = '0.3.1'.freeze end end asciidoctor-include-ext-0.3.1/lib/asciidoctor/include_ext/reader_ext.rb0000644000175000017500000000022313527456007025002 0ustar srudsrud# frozen_string_literal: true require 'asciidoctor' # Monkey-patch Reader to add #document. class Asciidoctor::Reader attr_reader :document end asciidoctor-include-ext-0.3.1/lib/asciidoctor/include_ext/tag_lines_selector.rb0000644000175000017500000001316213527456007026533 0ustar srudsrud# frozen_string_literal: true require 'logger' require 'set' require 'asciidoctor' require 'asciidoctor/include_ext/version' require 'asciidoctor/include_ext/logging' module Asciidoctor::IncludeExt # Lines selector that selects lines of the content based on the specified tags. # # @note Instance of this class can be used only once, as a predicate to # filter a single include directive. # # @example # include::some-file.adoc[tags=snippets;!snippet-b] # include::some-file.adoc[tag=snippets] # # @example # selector = TagLinesSelector.new("some-file.adoc", {"tag" => "snippets"}) # IO.foreach(filename).select.with_index(1, &selector) # # @see http://asciidoctor.org/docs/user-manual#by-tagged-regions class TagLinesSelector # @return [Integer, nil] 1-based line number of the first included line, # or `nil` if none. attr_reader :first_included_lineno # @param attributes [Hash] the attributes parsed from the # `include::[]`s attributes slot. # @return [Boolean] `true` if the *attributes* hash contains a key `"tag"` # or `"tags"`. def self.handles?(_, attributes) attributes.key?('tag') || attributes.key?('tags') end # @param target [String] name of the source file to include as specified # in the target slot of the `include::[]` directive. # @param attributes [Hash] the attributes parsed from the # `include::[]`s attributes slot. It must contain a key `"tag"` or `"tags"`. # @param logger [Logger] 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 # Returns `true` if the given line should be included, `false` otherwise. # # @note This method modifies state of this object. It's supposed to be # called successively with each line of the content being included. # See {TagLinesSelector example}. # # @param line [String] # @param line_num [Integer] 1-based *line* number. # @return [Boolean] `true` to select the *line*, `false` to reject. 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 # @return [Proc] {#include?} method as a Proc. def to_proc method(:include?).to_proc end protected attr_reader :logger, :target # @return [String, nil] a name of the active tag (region), or `nil` if none. def active_tag @stack.last.first end # @param tag_name [String] # @param _line_num [Integer] def enter_region!(tag_name, _line_num) if @tag_flags.key? tag_name @used_tags << tag_name @state = @tag_flags[tag_name] @stack << [tag_name, @state] elsif !@wildcard.nil? @state = active_tag && !@state ? false : @wildcard @stack << [tag_name, @state] end end # @param tag_name [String] # @param line_num [Integer] def exit_region!(tag_name, line_num) # valid end tag if tag_name == active_tag @stack.pop @state = @stack.last[1] # mismatched/unexpected end tag elsif @tag_flags.key? tag_name log_prefix = "#{target}: line #{line_num}" if (idx = @stack.rindex { |key, _| key == tag_name }) @stack.delete_at(idx) logger.warn "#{log_prefix}: mismatched end tag include: expected #{active_tag}, found #{tag_name}" # rubocop:disable LineLength else logger.warn "#{log_prefix}: unexpected end tag in include: #{tag_name}" end end end # Parses `tag::[]` and `end::[]` in the given *line*. # # @param line [String] # @return [Array, nil] a tuple `[Symbol, String]` where the first item is # `:start` or `:end` and the second is a tag name. If no tag is matched, # then `nil` is returned. def parse_tag_directive(line) @tag_directive_rx.match(line) do |m| [m[1].nil? ? :start : :end, m[2]] end end # @param tags_def [String] a comma or semicolon separated names of tags to # be selected, or rejected if prefixed with "!". # @param single [Boolean] whether the *tags_def* should be parsed as # a single tag name (i.e. without splitting on comma/semicolon). # @return [Hash] a Hash with tag names as keys and boolean # flags as values. def parse_attribute(tags_def, single = false) atoms = single ? [tags_def] : tags_def.split(/[,;]/) atoms.each_with_object({}) do |atom, tags| if atom.start_with? '!' tags[atom[1..-1]] = false if atom != '!' elsif !atom.empty? tags[atom] = true end end end end end asciidoctor-include-ext-0.3.1/README.adoc0000644000175000017500000000647213527456007016560 0ustar srudsrud= Asciidoctor Include Extension :source-language: shell // custom :gem-name: asciidoctor-include-ext :gh-name: jirutka/{gem-name} :gh-branch: master :codacy-id: 45320444129044688ef6553821b083f1 ifdef::env-github[] image:https://travis-ci.org/{gh-name}.svg?branch={gh-branch}[Build Status, link="https://travis-ci.org/{gh-name}"] image:https://api.codacy.com/project/badge/Coverage/{codacy-id}["Test Coverage", link="https://www.codacy.com/app/{gh-name}"] image:https://api.codacy.com/project/badge/Grade/{codacy-id}["Codacy Code quality", link="https://www.codacy.com/app/{gh-name}"] image:https://img.shields.io/gem/v/{gem-name}.svg?style=flat[Gem Version, link="https://rubygems.org/gems/{gem-name}"] image:https://img.shields.io/badge/yard-docs-blue.svg[Yard Docs, link="http://www.rubydoc.info/github/{gh-name}/{gh-branch}"] endif::env-github[] This project is a reimplementation of the http://asciidoctor.org[Asciidoctor]’s built-in (pre)processor for the http://asciidoctor.org/docs/user-manual/#include-directive[include::[\]] directive in extensible and more clean way. It provides the same features, but you can easily adjust it or extend for your needs. For example, you can change how it loads included files or add another ways how to select portions of the document to include. == Why? You may ask why I _reimplemented_ something that is already in the Asciidoctor core. Well… Code for decision if the include is allowed, parsing attributes for partial selection, reading the file to be included, filtering its content according to `lines` or `tags` attribute, handling errors… all of this is implemented directly in a single 210 lines long method https://github.com/asciidoctor/asciidoctor/blob/911d0bd509f369e9da15d2bb71f81aecb7c45fec/lib/asciidoctor/reader.rb#L824-L1034[Asciidoctor::Reader#preprocess_include_directive] with really horrible perl-like spaghetti code. :spaghetti: :hankey: How can you adjust it or reuse outside of the Asciidoctor codebase? For example, what if you can’t read documents directly from file system? Then you’re out of luck. There’s no way how to do that without reimplementing this whole mess on your own (monkey-patching `Kernel.open` and `File.file?` is not a sensible option…). I wrote this extension to allow implementing a complete support of `include::[]` directive in GitLab. And also to open doors for adding some custom _selectors_, e.g. selecting lines using regular expression in addition to ranges of line numbers and tags. == Installation To install (or update to the latest version): [source, subs="+attributes"] gem install {gem-name} or to install the latest development version: [source, subs="+attributes"] gem install {gem-name} --pre == Usage Just `require '{gem-name}'`. If you invoke Asciidoctor from command-line, use option `-r` to load the extension: [source, subs="+attributes"] asciidoctor -r {gem-name} README.adoc If you don’t want the extension to be automatically registered in Asciidoctor, don’t _require_ `{gem-name}`, but `asciidoctor/include_ext/include_processor`. IMPORTANT: Bundler automatically _requires_ all the specified gems. To prevent it, use `gem '{gem-name}', require: false`. == License This project is licensed under http://opensource.org/licenses/MIT/[MIT License]. For the full text of the license, see the link:LICENSE[LICENSE] file.