Sha256: 892ea1115b1d06149a8b98624cf6930193577ee8a3ec358dad2098d80ccbed34

Contents?: true

Size: 977 Bytes

Versions: 3

Compression:

Stored size: 977 Bytes

Contents

# frozen_string_literal: true

class LivingDocument::DocumentEvaluator
  def initialize(document:)
    @document = document.dup
  end

  def evaluated_document
    if markdown?
      markdown_codeblocks.each do |markdown_codeblock|
        opening, *ruby_code_lines, closing = markdown_codeblock.split("\n")
        ruby_code = ruby_code_lines.join("\n")
        evaluated_ruby_code =
          LivingDocument::CodeEvaluator.new(
            code: ruby_code,
          ).evaluated_code
        @document[markdown_codeblock] =
          <<~EVALUATED_CODEBLOCK.rstrip
            #{opening}
            #{evaluated_ruby_code}
            #{closing}
          EVALUATED_CODEBLOCK
      end
      @document
    else
      LivingDocument::CodeEvaluator.new(
        code: @document,
      ).evaluated_code
    end
  end

  def markdown?
    !markdown_codeblocks.empty?
  end

  private

  def markdown_codeblocks
    @document.scan(/```(?:ruby|rb)\n(?:(?!```\n).)*```/mi)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
living_document-1.1.1 lib/living_document/document_evaluator.rb
living_document-1.1.0 lib/living_document/document_evaluator.rb
living_document-1.0.0 lib/living_document/document_evaluator.rb