Sha256: 219d51d7ca5592355a9e67f76be9ba511470fd2b2a4281fb0317f4663ba3182e

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 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

3 entries across 3 versions & 1 rubygems

Version Path
asciidoctor-pdf-1.5.0.beta.8 lib/asciidoctor/pdf/formatted_text/formatter.rb
asciidoctor-pdf-1.5.0.beta.7 lib/asciidoctor/pdf/formatted_text/formatter.rb
asciidoctor-pdf-1.5.0.beta.6 lib/asciidoctor/pdf/formatted_text/formatter.rb