Module: AsciidoctorExtensions::PlantUmlPreprocessor

Defined in:
lib/asciidoctor/extensions/asciidoctor_kroki/preprocess.rb

Overview

Resolves PlantUML !include directives (and friends) in diagram text before it is sent to the Kroki server, mirroring the JavaScript/Node.js extension's preprocessor (src/preprocess.js) so both language bindings behave the same way. See http://plantuml.com/en/preprocessing.

Unlike the JS extension (which also supports Antora resource IDs and a pluggable virtual filesystem), this Ruby port only needs to resolve plain local paths and http(s) URLs, since the Ruby gem is not used with Antora. rubocop:disable Metrics/ModuleLength

Constant Summary collapse

PLANTUML_BLOCK_RX =
/@startuml\r?\n([\s\S]*?)\r?\n@enduml/.freeze
INCLUDE_LINE_RX =
/^\s*!(include(?:_many|_once|url|sub)?)\s+(.*)/.freeze

Class Method Summary collapse

Class Method Details

.preprocess(diagram_text, resource_path, include_paths, logger) ⇒ String

Returns the diagram text with !include directives resolved.

Parameters:

  • diagram_text (String)

    the raw diagram source

  • resource_path (String, nil)

    absolute path (or URL) of the file the diagram text was read from, used to resolve relative !include directives; nil for a diagram written directly in the AsciiDoc source (resolved relative to the current working directory, like plain !include paths without a match in kroki-plantuml-include-paths)

  • include_paths (String, nil)

    the kroki-plantuml-include-paths attribute value (a list of directories separated by File::PATH_SEPARATOR)

  • logger (#info)

    logger used to report includes that were skipped, not raised

Returns:

  • (String)

    the diagram text with !include directives resolved



29
30
31
32
33
34
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/preprocess.rb', line 29

def preprocess(diagram_text, resource_path, include_paths, logger)
  resource = resource_path ? parse_resource(resource_path) : { dir: '', path: nil }
  paths = include_paths.to_s.empty? ? [] : include_paths.split(::File::PATH_SEPARATOR)
  diagram_text = preprocess_includes(diagram_text, resource, [], [], paths, logger)
  remove_tags(diagram_text)
end