# File lib/asciidoctor/parser.rb, line 87
  def self.parse_document_header(reader, document)
    # capture lines of block-level metadata and plow away 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 (has_doctitle_line = is_next_line_document_title?(reader, block_attributes)) &&
        block_attributes.has_key?('title')
      return document.finalize_header block_attributes, false
    end

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

    section_title = nil
    # if the first line is the document title, add a header to the document and parse the header metadata
    if has_doctitle_line
      source_location = reader.cursor if document.sourcemap
      document.id, _, doctitle, _, single_line = parse_section_title reader, document
      unless assigned_doctitle
        document.title = assigned_doctitle = doctitle
      end
      # default to compat-mode if document uses atx-style doctitle
      document.set_attribute 'compat-mode', '' unless single_line
      if (separator = block_attributes.delete 'separator')
        document.set_attribute 'title-separator', separator
      end
      document.header.source_location = source_location if source_location
      document.attributes['doctitle'] = section_title = doctitle
      # QUESTION: should the id assignment on Document be encapsulated in the Document class?
      if document.id
        block_attributes.delete 1
        block_attributes.delete 'id'
      else
        if (style = block_attributes.delete 1)
          style_attrs = { 1 => style }
          parse_style_attribute style_attrs, reader
          block_attributes['id'] = style_attrs['id'] if style_attrs.key? 'id'
        end
        document.id = block_attributes.delete 'id'
      end
      parse_header_metadata reader, document
    end

    unless (val = document.attributes['doctitle']).nil_or_empty? || val == section_title
      document.title = assigned_doctitle = val
    end

    # restore doctitle attribute to original assignment
    document.attributes['doctitle'] = assigned_doctitle if assigned_doctitle

    # parse title and consume name section of manpage document
    parse_manpage_header(reader, document) if document.doctype == 'manpage'

    # NOTE block_attributes are the block-level attributes (not document attributes) that
    # precede the first line of content (document title, first section or first block)
    document.finalize_header block_attributes
  end