From 3d51e447e2b2dfd8662048de0d53f627067f0fa7 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sat, 14 Apr 2018 03:33:26 -0600 Subject: resolves #2387 allow role to be set on document - allow role to be set on document using block attribute (shorthand or longhand) - assign role to docrole attribute on document - add role to CSS classes on body in HTML5 output --- lib/asciidoctor/converter/html5.rb | 9 +++++---- lib/asciidoctor/parser.rb | 3 +++ test/sections_test.rb | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/asciidoctor/converter/html5.rb b/lib/asciidoctor/converter/html5.rb index 5bba5642..be39a11d 100644 --- a/lib/asciidoctor/converter/html5.rb +++ b/lib/asciidoctor/converter/html5.rb @@ -115,13 +115,14 @@ module Asciidoctor end result << '' - body_attrs = [] - body_attrs << %(id="#{node.id}") if node.id + body_attrs = node.id ? [%(id="#{node.id}")] : [] if (sectioned = node.sections?) && (node.attr? 'toc-class') && (node.attr? 'toc') && (node.attr? 'toc-placement', 'auto') - body_attrs << %(class="#{node.doctype} #{node.attr 'toc-class'} toc-#{node.attr 'toc-position', 'header'}") + classes = [node.doctype, (node.attr 'toc-class'), %(toc-#{node.attr 'toc-position', 'header'})] else - body_attrs << %(class="#{node.doctype}") + classes = [node.doctype] end + classes << (node.attr 'docrole') if node.attr? 'docrole' + body_attrs << %(class="#{classes * ' '}") body_attrs << %(style="max-width: #{node.attr 'max-width'};") if node.attr? 'max-width' result << %() diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb index 72410229..45e76798 100644 --- a/lib/asciidoctor/parser.rb +++ b/lib/asciidoctor/parser.rb @@ -157,6 +157,9 @@ class Parser else doc_id = document.id end + if (doc_role = block_attributes['role']) + document.attributes['docrole'] = doc_role + end if (doc_reftext = block_attributes['reftext']) document.attributes['reftext'] = doc_reftext end diff --git a/test/sections_test.rb b/test/sections_test.rb index 8194104f..c98e9c41 100644 --- a/test/sections_test.rb +++ b/test/sections_test.rb @@ -466,8 +466,10 @@ content doc = document_from_string input assert_empty doc.blocks[0].attributes output = doc.convert + assert_css '#idname', output, 1 assert_css 'body#idname', output, 1 - assert_css '.rolename', output, 0 + assert_css '.rolename', output, 1 + assert_css 'body.rolename', output, 1 end end -- cgit v1.2.3 From 60dd2aae617f3f88cfbfb31edec1237c97238fcb Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Thu, 19 Apr 2018 01:57:44 -0600 Subject: assign document attributes consistently in Parser.parse_document_header - store document attributes in local variable (doc_attrs) - assign attributes directly to doc_attrs instead of using doc.set_attr - rename block_attributes to block_attrs - assign source_location immediately after setting title (which creates header block) --- lib/asciidoctor/parser.rb | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb index 45e76798..5ebfd137 100644 --- a/lib/asciidoctor/parser.rb +++ b/lib/asciidoctor/parser.rb @@ -122,19 +122,19 @@ class Parser # returns the Hash of orphan block attributes captured above the header 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 + block_attrs = parse_block_metadata_lines reader, document + doc_attrs = document.attributes # special case, block title is not allowed above document title, # carry attributes over to the document body - if (implicit_doctitle = is_next_line_doctitle? reader, block_attributes, document.attributes['leveloffset']) && - (block_attributes.key? 'title') - return document.finalize_header block_attributes, false + if (implicit_doctitle = is_next_line_doctitle? reader, block_attrs, doc_attrs['leveloffset']) && block_attrs['title'] + return document.finalize_header block_attrs, 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? + unless (val = doc_attrs['doctitle']).nil_or_empty? document.title = assigned_doctitle = val end @@ -142,45 +142,43 @@ class Parser if implicit_doctitle source_location = reader.cursor if document.sourcemap document.id, _, doctitle, _, atx = parse_section_title reader, document - unless assigned_doctitle - document.title = assigned_doctitle = doctitle - end + document.title = assigned_doctitle = doctitle unless assigned_doctitle + document.header.source_location = source_location if source_location # default to compat-mode if document uses atx-style doctitle - document.set_attr 'compat-mode' unless atx || (document.attribute_locked? 'compat-mode') - if (separator = block_attributes['separator']) - document.set_attr 'title-separator', separator unless document.attribute_locked? 'title-separator' + doc_attrs['compat-mode'] = '' unless atx || (document.attribute_locked? 'compat-mode') + if (separator = block_attrs['separator']) + doc_attrs['title-separator'] = separator unless document.attribute_locked? 'title-separator' end - document.header.source_location = source_location if source_location - document.attributes['doctitle'] = section_title = doctitle - if (doc_id = block_attributes['id']) + doc_attrs['doctitle'] = section_title = doctitle + if (doc_id = block_attrs['id']) document.id = doc_id else doc_id = document.id end - if (doc_role = block_attributes['role']) - document.attributes['docrole'] = doc_role + if (doc_role = block_attrs['role']) + doc_attrs['docrole'] = doc_role end - if (doc_reftext = block_attributes['reftext']) - document.attributes['reftext'] = doc_reftext + if (doc_reftext = block_attrs['reftext']) + doc_attrs['reftext'] = doc_reftext end - block_attributes = {} + block_attrs = {} parse_header_metadata reader, document document.register :refs, [doc_id, document] if doc_id end - unless (val = document.attributes['doctitle']).nil_or_empty? || val == section_title + unless (val = doc_attrs['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 + doc_attrs['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 + # NOTE block_attrs 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 + document.finalize_header block_attrs end # Public: Parses the manpage header of the AsciiDoc source read from the Reader -- cgit v1.2.3