<commit_before>using TextWrap

#Format the executed document
function format(executed, doctype)
  formatted = String[]
  docformat = formats[doctype]
  #@show docformat

  #Complete format dictionaries with defaults
  formatdict = docformat.formatdict
  get!(formatdict, :termstart, formatdict[:codestart])
  get!(formatdict, :termend, formatdict[:codeend])
  get!(formatdict, :out_width, nothing)
  get!(formatdict, :out_height, nothing)
  get!(formatdict, :fig_pos, nothing)
  get!(formatdict, :fig_env, nothing)


  for chunk in copy(executed)

      if chunk[:type] == "doc"
          push!(formatted, chunk[:content])
      else
          #Fill undefined options with format specific defaults
          chunk[:out_width] == nothing && (chunk[:out_width] =  docformat.formatdict[:out_width])
          chunk[:fig_pos] == nothing && (chunk[:fig_pos] =  docformat.formatdict[:fig_pos])

          #Only use floats if chunk has caption or sets fig_env
          if chunk[:fig_cap] != nothing && chunk[:fig_env] == nothing
              (chunk[:fig_env] =  docformat.formatdict[:fig_env])
          end




          #Format code
          result = format_codechunk(chunk, formatdict)
          #Handle figures
          if chunk[:fig] && length(chunk[:figure]) > 0
              if chunk[:include]
                  result *= formatfigures(chunk, docformat)
              end
          end
          push!(formatted, result)
      end
  end

 return formatted
end


function format_codechunk(chunk, formatdict)
    if haskey(formatdict, :indent)
        chunk[:content] = indent(chunk[:content], formatdict[:indent])
    end

    if !chunk[:eval]
        if chunk[:echo]
            result = "$(formatdict[:codestart])$(chunk[:content])\n$(formatdict[:codeend])"
            return result
        else
            r = ""
            return r
        end
    end

    if chunk[:term]
        result = format_termchunk(chunk, formatdict)
    else
        if chunk[:wrap]
          chunk[:result] = "\n" * wrap(chunk[:result], replace_whitespace=false)
        end

        if chunk[:echo]
            result = "$(formatdict[:codestart])$(chunk[:content])\n$(formatdict[:codeend])\n"
        else
            result = ""
        end

        if (strip(chunk[:result])!= "") && (chunk[:results] != "hidden")
            #@show chunk
            if chunk[:results] != "markup"
                result *= "$(chunk[:result])"
            elseif chunk[:results] == "markup"
                if haskey(formatdict, :indent)
                    chunk[:result] = indent(chunk[:result], formatdict[:indent])
                end
                result *= "$(formatdict[:outputstart])$(chunk[:result])\n$(formatdict[:outputend])\n"
            end
        end

    end

    return result

end

function format_termchunk(chunk, formatdict)
    if chunk[:echo] && chunk[:results] != "hidden"
        result = "$(formatdict[:termstart])$(chunk[:result])\n"
        #@show chunk[:term_state]
        chunk[:term_state] == :text && (result*= "$(formatdict[:termend])\n")
    else
        result = ""
    end
    return result
end

function indent(text, nindent)<commit_msg>Formatting code fixed, apart from figures<commit_after>using TextWrap

#Format the executed document
function format(executed, doctype)
    formatted = String[]
    docformat = formats[doctype]
    #@show docformat

    #Complete format dictionaries with defaults
    formatdict = docformat.formatdict
    get!(formatdict, :termstart, formatdict[:codestart])
    get!(formatdict, :termend, formatdict[:codeend])
    get!(formatdict, :out_width, nothing)
    get!(formatdict, :out_height, nothing)
    get!(formatdict, :fig_pos, nothing)
    get!(formatdict, :fig_env, nothing)


    for chunk in copy(executed)
        result = format_chunk(chunk, formatdict)
        push!(formatted, result)
    end

    return formatted
end


function format_chunk(chunk::DocChunk, formatdict)
    return chunk.content
end


function format_chunk(chunk::CodeChunk, formatdict)
    #Fill undefined options with format specific defaults
    chunk.options[:out_width] == nothing &&
        (chunk.options[:out_width] =  formatdict[:out_width])
    chunk.options[:fig_pos] == nothing &&
        (chunk.options[:fig_pos] =  formatdict[:fig_pos])

    #Only use floats if chunk has caption or sets fig_env
    if chunk.options[:fig_cap] != nothing && chunk.options[:fig_env] == nothing
        (chunk.options[:fig_env] =  formatdict[:fig_env])
    end


    if haskey(formatdict, :indent)
        chunk.content = indent(chunk.content, formatdict[:indent])
    end

    if !chunk.options[:eval]
        if chunk.options[:echo]
            result = "$(formatdict[:codestart])$(chunk.content)\n$(formatdict[:codeend])"
            return result
        else
            r = ""
            return r
        end
    end

    if chunk.options[:term]
        result = format_termchunk(chunk, formatdict)
    else
        if chunk.options[:wrap]
          chunk.output = "\n" * wrap(chunk.output, replace_whitespace=false)
        end

        if chunk.options[:echo]
            result = "$(formatdict[:codestart])$(chunk.content)\n$(formatdict[:codeend])\n"
        else
            result = ""
        end

        if (strip(chunk.output)!= "") && (chunk.options[:results] != "hidden")
            #@show chunk
            if chunk.options[:results] != "markup"
                result *= "$(chunk.output)"
            elseif chunk.options[:results] == "markup"
                if haskey(formatdict, :indent)
                    chunk.output = indent(chunk.output, formatdict[:indent])
                end
                result *= "$(formatdict[:outputstart])$(chunk.output)\n$(formatdict[:outputend])\n"
            end
        end

    end

    #Handle figures
    if chunk.options[:fig] && length(chunk.figures) > 0
        if chunk.options[:include]
            result *= formatfigures(chunk, docformat)
        end
    end

    return result
end

function format_termchunk(chunk, formatdict)
    if chunk.options[:echo] && chunk.options[:results] != "hidden"
        result = "$(formatdict[:termstart])$(chunk.output)\n"
        #@show chunk[:term_state]
        chunk.options[:term_state] == :text && (result*= "$(formatdict[:termend])\n")
    else
        result = ""
    end
    return result
end

function indent(text, nindent)
{'fraction_non_alphanumeric': 0.12997013987112996, 'fraction_numerical': 0.0003143171460003143, 'mean_word_length': 2.577290612703766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6371659', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1788, 'n_words': 411}