Module: Asciidoctor::Html::Pagination

Included in:
Book
Defined in:
lib/asciidoctor/html/pagination.rb

Overview

Mixin to add pagination support to Book class

Defined Under Namespace

Classes: PagItem

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.chevron(left: false) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/asciidoctor/html/pagination.rb', line 10

def self.chevron(left: false)
  transform_attr = %( transform="rotate(180 7 8)") if left
  <<~HTML
    <svg xmlns="http://www.w3.org/2000/svg" width="100%" fill="currentColor" viewBox="3 0 8 16">
      <path#{transform_attr} fill-rule="evenodd" d="M6.776 1.553a.5.5 0 0 1 .671.223l3 6a.5.5 0 0 1 0 .448l-3 6a.5.5 0 1 1-.894-.448L9.44 8 6.553 2.224a.5.5 0 0 1 .223-.671"/>
    </svg>
  HTML
end

.display_prefix(prefix) ⇒ Object



37
38
39
# File 'lib/asciidoctor/html/pagination.rb', line 37

def self.display_prefix(prefix)
  %(<span class="title-prefix">#{prefix}</span><br>)
end

.pagitem_html(text, left: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/asciidoctor/html/pagination.rb', line 19

def self.pagitem_html(text, left: false)
  if left
    <<~HTML
      <div class="chevron">
        #{Pagination.chevron left:}
      </div>
      <div class="pagination-prv">#{text}</div>
    HTML
  else
    <<~HTML
      <div class="pagination-nxt">#{text}</div>
      <div class="chevron">
        #{Pagination.chevron}
      </div>
    HTML
  end
end

Instance Method Details

#display_paginator(prv, nxt) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/asciidoctor/html/pagination.rb', line 41

def display_paginator(prv, nxt)
  blank = %(<span class="blank">&nbsp;</span>)
  html = [<<~HTML
    <div class="paginator-wrapper">
    <div class="paginator dynamic-width">
  HTML
  ]
  html << if prv
            prefix = Pagination.display_prefix(prv.title) if prv.title
            text = "#{prefix}#{prv.text}"
            <<~HTML
              <a id="flip-back" href="#{prv.url}">
                #{Pagination.pagitem_html text, left: true}
              </a>
            HTML
          else
            blank
          end
  html << if nxt
            prefix = Pagination.display_prefix(nxt.title) if nxt.title
            text = "#{prefix}#{nxt.text}"
            <<~HTML
              <a id="flip-forward" href="#{nxt.url}">
                #{Pagination.pagitem_html text}
              </a>
            HTML
          else
            blank
          end
  html << %(</div></div>)
  html.join("\n")
end

#pagination(key = -1)) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/asciidoctor/html/pagination.rb', line 92

def pagination(key = -1)
  keys = @refs.keys
  idx = keys.find_index key
  return "" unless idx

  prv_nxt keys, idx
end

#prv_nxt(keys, idx) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/asciidoctor/html/pagination.rb', line 74

def prv_nxt(keys, idx)
  pagitems = []
  [idx - 1, idx + 1].each do |i|
    if i.between?(0, keys.size - 1)
      key = keys[i]
      tdata = @templates[key]
      pagitems << PagItem.new(
        url: "#{key}.html",
        title: tdata[:chapheading],
        text: tdata[:chapsubheading]
      )
    else
      pagitems << nil
    end
  end
  display_paginator(*pagitems)
end