Sha256: f785eff94644847536d1b90b7ccc944cad2539eea132f705a62764fce2d4852b

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Asciidoctor
  module PDF
    module FormattedText
      class Formatter
        if defined? ::Asciidoctor::Logging
          include ::Asciidoctor::Logging
        else
          include ::Asciidoctor::LoggingShim
        end

        FormattingSnifferPattern = /[<&]/
        WHITESPACE = %( \t\n)

        def initialize options = {}
          @parser = MarkupParser.new
          @transform = Transform.new merge_adjacent_text_nodes: true, theme: options[:theme]
        end

        def format string, *args
          options = args[0] || {}
          string = string.tr_s WHITESPACE, ' ' if options[:normalize]
          inherited = options[:inherited]
          if FormattingSnifferPattern.match? string
            if (parsed = @parser.parse string)
              return @transform.apply parsed.content, [], inherited
            else
              logger.error %(failed to parse formatted text: #{string})
            end
          end
          [inherited ? (inherited.merge text: string) : { text: string }]
        end

        # The original purpose of this method is to split paragraphs, but our formatter only works on paragraphs that have
        # been presplit. Therefore, we just need to wrap the fragments in a single-element array (representing a single
        # paragraph) and return them.
        def array_paragraphs fragments
          [fragments]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
asciidoctor-pdf-1.5.0.rc.2 lib/asciidoctor/pdf/formatted_text/formatter.rb
asciidoctor-pdf-1.5.0.rc.1 lib/asciidoctor/pdf/formatted_text/formatter.rb