Class: Asciidoctor::Rhrev::ChangeBarsHtmlTreeprocessor

Inherits:
Extensions::Treeprocessor
  • Object
show all
Defined in:
lib/asciidoctor/rhrev/rhrev_html.rb

Overview

Tags blocks marked with the current revision (revnumber 1.2 => attribute rhrev1-2) with the rhrev-changed role so a stylesheet can draw an HTML change bar (border on the right side). Enabled by the rhrev-change-bars document attribute, mirroring the PDF backend. The rhrev-change-bars-color, -width, and -offset attributes are emitted as CSS custom properties; the side is always right in HTML output (no recto/verso model).

Constant Summary collapse

TRACKED_CONTEXTS =
[:section, :floating_title, :example, :listing, :table, :image].freeze

Instance Method Summary collapse

Instance Method Details

#process(document) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/asciidoctor/rhrev/rhrev_html.rb', line 17

def process document
  return document unless document.basebackend? 'html'
  return document unless document.attr? 'rhrev-change-bars'
  return document unless (revnumber = document.attr 'revnumber')
  prefix = document.attr 'revhistoryprefix', 'rhrev'
  change_attr = %(#{prefix}#{revnumber.to_s.tr '.', '-'})

  tagged = false
  document.find_by { |b| TRACKED_CONTEXTS.include? b.context }.each do |block|
    next unless block.attributes && (block.attributes.key? change_attr)
    roles = (block.attr 'role')
    block.set_attr 'role', [roles, 'rhrev-changed'].compact.join(' ')
    tagged = true
  end
  return document unless tagged

  overrides = []
  if (color = document.attr 'rhrev-change-bars-color')
    overrides << %(--rhrev-change-bar-color: ##{color.to_s.delete_prefix '#'};)
  end
  if (width = document.attr 'rhrev-change-bars-width')
    overrides << %(--rhrev-change-bar-width: #{width}px;)
  end
  if (offset = document.attr 'rhrev-change-bars-offset')
    overrides << %(--rhrev-change-bar-offset: #{offset}px;)
  end
  unless overrides.empty?
    style = create_block document, :pass, %(<style>:root { #{overrides.join ' '} }</style>), {}
    document.blocks.unshift style
  end
  document
end