# File lib/asciidoctor/lexer.rb, line 72
  def self.parse_document_header(reader, document)
    # capture any lines of block-level metadata and plow away any comment lines
    # that precede first block
    block_attributes = parse_block_metadata_lines(reader, document)

    # special case, block title is not allowed above document title,
    # carry attributes over to the document body
    if block_attributes.has_key?('title')
      document.clear_playback_attributes block_attributes
      document.save_attributes
      block_attributes['invalid-header'] = true
      return block_attributes
    end

    # yep, document title logic in AsciiDoc is just insanity
    # definitely an area for spec refinement
    assigned_doctitle = nil
    unless (val = document.attributes.fetch('doctitle', '')).empty?
      document.title = val
      assigned_doctitle = val
    end

    section_title = nil
    # check if the first line is the document title
    # if so, add a header to the document and parse the header metadata
    if is_next_line_document_title?(reader, block_attributes)
      document.id, doctitle, _, _ = parse_section_title(reader, document)
      unless assigned_doctitle
        document.title = doctitle
        assigned_doctitle = doctitle
      end
      document.attributes['doctitle'] = section_title = doctitle
      # QUESTION: should this be encapsulated in document?
      if document.id.nil? && block_attributes.has_key?('id')
        document.id = block_attributes.delete('id')
      end
      parse_header_metadata(reader, document)
    end

    if !(val = document.attributes.fetch('doctitle', '')).empty? &&
        val != section_title
      document.title = val
      assigned_doctitle = val
    end

    # restore doctitle attribute to original assignment
    if assigned_doctitle
      document.attributes['doctitle'] = assigned_doctitle
    end
 
    document.clear_playback_attributes block_attributes
    document.save_attributes
 
    # NOTE these are the block-level attributes (not document attributes) that
    # precede the first line of content (document title, first section or first block)
    block_attributes
  end