Module: Asciidoctor::Html::BookTemplate

Defined in:
lib/asciidoctor/html/book_template.rb

Overview

The template for the book layout

Constant Summary collapse

<<~HTML
  <button type="button" id="menu-btn" class="btn menu"
          aria-expanded="false" aria-controls="sidebar">
    <i class="bi bi-list"></i>
  </button>
HTML

Class Method Summary collapse

Class Method Details

.appendix_title(chapname, numeral, doctitle, num_appendices) ⇒ Object



51
52
53
54
# File 'lib/asciidoctor/html/book_template.rb', line 51

def self.appendix_title(chapname, numeral, doctitle, num_appendices)
  numeral = num_appendices == 1 ? "" : " #{numeral}"
  %(<span class="title-prefix">#{chapname}#{numeral}</span>#{doctitle})
end

.chapheader(chapheading, chapsubheading) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/asciidoctor/html/book_template.rb', line 94

def self.chapheader(chapheading, chapsubheading)
  chapheading_suffix = ": " if chapheading
  <<~HTML
    <div class="breadcrumb">
      <a href="#page" class="link-offset-2 link-underline-opacity-50 link-underline-opacity-100-hover">
        #{chapheading}#{chapheading_suffix}#{chapsubheading}
      </a>
    </div>
  HTML
end

.chapheading(text) ⇒ Object



105
106
107
# File 'lib/asciidoctor/html/book_template.rb', line 105

def self.chapheading(text)
  %(<h1 class="chapheading">#{text}</h1>) if text
end


169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/asciidoctor/html/book_template.rb', line 169

def self.footer(authors)
  <<~HTML
    <footer class="footer">
      <div class="footer-content dynamic-width">
        <div class="footer-left">&#169; <span id="cr-year"></span> #{authors}</div>
        <div class="footer-right">Built with
          <a href="https://github.com/ravirajani/asciidoctor-html">asciidoctor-html</a>
        </div>
      </div>
    </footer>
  HTML
end

.head(opts) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/asciidoctor/html/book_template.rb', line 198

def self.head(opts)
  <<~HTML
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    #{%(<meta name="description" content="#{opts[:description]}">) if opts[:description]}
    #{%(<meta name="author" content="#{opts[:authors]}">) if opts[:authors]}
    <title>#{opts[:title]}</title>
    <link rel="icon" type="image/png" href="#{FAVICON_PATH}/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/svg+xml" href="#{FAVICON_PATH}/favicon.svg">
    <link rel="shortcut icon" href="#{FAVICON_PATH}/favicon.ico">
    <link rel="apple-touch-icon" sizes="180x180" href="#{FAVICON_PATH}/apple-touch-icon.png">
    <meta name="apple-mobile-web-app-title" content="#{opts[:short_title]}">
    <link rel="manifest" href="site.webmanifest">
    <link rel="stylesheet" href="#{CSS_PATH}/styles.css">
    #{highlightjs(opts)}
    <script>
      MathJax = {
        tex: {
          inlineMath: {'[+]': [['$', '$']]}
        },
        output: {
          displayOverflow: 'linebreak'
        }
      };
      ADHT = {}; // Custom namespace.
    </script>
    <script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-chtml.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.6/dist/js/bootstrap.bundle.min.js"
          integrity="sha384-j1CDi7MgGQ12Z7Qab0qlWQ/Qqz24Gc6BM0thvEMVjHnfYGF0rmFCozFSxQBxwHKO"
          crossorigin="anonymous"></script>
  HTML
end

.header(title, short_title, chapheading, has_subnav) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/asciidoctor/html/book_template.rb', line 152

def self.header(title, short_title, chapheading, has_subnav)
  sublink = <<~HTML
    <span id="header-chapheading" class="home-sublink"><a class="home" href="#page">#{chapheading}</a></span>
  HTML
  <<~HTML
    <header class="header#{" with-margin" unless has_subnav}">
      <div class="dynamic-width">
        <div class="home-container">
          <a class="home d-none d-sm-block" href="./">#{title}</a>
          <a class="home d-block d-sm-none" href="./">#{short_title}</a>
          #{sublink if chapheading}
        </div>
      </div>
    </header>
  HTML
end

.highlightjs(opts) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/asciidoctor/html/book_template.rb', line 182

def self.highlightjs(opts)
  return "" unless opts[:langs]

  html = [
    <<~HTML
      <link rel="stylesheet" href="#{Highlightjs::CDN_PATH}/styles/tomorrow-night-blue.min.css">
      <script defer src="#{Highlightjs::CDN_PATH}/highlight.min.js"></script>
    HTML
  ]
  opts[:langs].each do |lang|
    html << %(<script defer src="#{Highlightjs::CDN_PATH}/languages/#{lang}.min.js"></script>)
  end
  html << %(<script defer src="#{Highlightjs::LN_PLUGIN_PATH}"></script>) if opts[:linenumbering]
  html.join "\n"
end

.html(content, nav_items, opts = {}) ⇒ Object

opts:

  • has_subnav: Boolean
  • has_live: Boolean
  • title: String
  • short_title: String
  • authors: String
  • description: String
  • chapheading: String
  • chapsubheading: String
  • langs: Array
  • at_head_end: String
  • at_body_end: String
  • paginator: String
  • pagestyle: Symbol(single|multi|presentation)


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/asciidoctor/html/book_template.rb', line 245

def self.html(content, nav_items, opts = {})
  nav = !nav_items.empty? && (nav_items.size > 1 || opts[:has_subnav])
  page_classes = ["page"]
  if nav
    page_classes << "multi" unless opts[:pagestyle] == :single
    page_classes << "presentation" if opts[:pagestyle] == :presentation
  end
  page_classname = page_classes.join " "
  <<~HTML
    <!DOCTYPE html>
    <html lang="en">
    <head>
    #{head(opts)}
    #{opts[:at_head_end]}
    </head>
    <body>
    #{sidebar(nav_items) if nav}
    <div id="page" class="#{page_classname}">
    #{MENU_BTN if nav}
    #{header opts[:title], opts[:short_title], opts[:chapheading], opts[:has_subnav]}
    #{main content:, **opts}
    #{opts[:paginator]}
    #{footer opts[:authors]}
    </div> <!-- .page -->
    <script>document.getElementById("cr-year").textContent = (new Date()).getFullYear();</script>
    <script type="module">
    #{Highlightjs::PLUGIN if opts[:langs]}
    #{Popovers::POPOVERS}
    #{Sidebar::TOGGLE if nav}
    #{Scroll::SCROLL}
    #{Flip::FLIP if nav}
    #{Live::LIVE if nav && opts[:has_live]}
    </script>
    #{opts[:at_body_end]}
    </body>
    </html>
  HTML
end

.main(opts) ⇒ Object

opts:

  • chapheading: String
  • chapsubheading: String
  • content: String
  • has_subnav: Boolean
  • authors: String
  • date: Date
  • pagestyle: Symbol


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/asciidoctor/html/book_template.rb', line 117

def self.main(opts)
  <<~HTML
    <main id="main" class="main">
    <div id="content-container" class="content-container dynamic-width"  tabindex="0">
    #{chapheader opts[:chapheading], opts[:chapsubheading]}
    <div class="chaphead flip d-block">
      #{toggle_button opts[:pagestyle] if opts[:has_subnav]}
      #{chapheading opts[:chapheading]}
      <h1 class="chaptitle">#{opts[:chapsubheading]}</h1>
      <p class="lead chapauthors#{%( d-block) if opts[:pagestyle] == :presentation}">#{opts[:authors]}</p>
    </div>
    #{opts[:content]}
    </div>
    </main>
  HTML
end


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/asciidoctor/html/book_template.rb', line 21

def self.nav_item(target, text, content = nil, active: false, line_number: 0)
  if content
    id = target.sub(/\.html$/, "-subnav").tr ".", "-"
    show_class = " show" if active
    subnav = %(<div class="collapse#{show_class}" id="#{id}">\n#{content}\n</div>)
    toggle = <<~HTML
      <button class="btn subnav-toggle#{" collapsed" unless active}"
              type="button"
              data-bs-toggle="collapse"
              data-bs-target="##{id}"
              aria-expanded="#{active ? "true" : "false"}"
              aria-controls="#{id}">
        <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="caret" viewBox="0 0 16 16">
          <path d="M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z"/>
        </svg>
      </button>
    HTML
  end
  active_class = %( class="active") if active
  link = %(<a href="#{target}">#{text}</a>#{toggle})
  live_attr = Utils.line_number_attr(line_number, live: line_number.positive?)
  %(<li#{active_class}#{live_attr}>#{link}#{subnav}</li>\n)
end


45
46
47
48
49
# File 'lib/asciidoctor/html/book_template.rb', line 45

def self.nav_text(chapprefix, chaptitle)
  return chaptitle if chapprefix.empty?

  %(<div class="nav-mark">#{chapprefix}</div>#{chaptitle})
end


56
57
58
59
60
61
62
63
64
65
# File 'lib/asciidoctor/html/book_template.rb', line 56

def self.sidebar(nav_items)
  <<~HTML
    <div id="sidebar" class="sidebar">
    <button id="sidebar-dismiss-btn" class="btn dismiss"><i class="bi bi-x-lg"></i></button>
    <nav><ul>
    #{nav_items.join "\n"}
    </ul></nav>
    </div> <!-- .sidebar -->
  HTML
end

.sitemap(entries) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/asciidoctor/html/book_template.rb', line 134

def self.sitemap(entries)
  <<~XML
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

    #{entries.join "\n"}
    </urlset>
  XML
end

.sitemap_entry(url) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/asciidoctor/html/book_template.rb', line 144

def self.sitemap_entry(url)
  <<~XML
    <url>
      <loc>#{url}</loc>
    </url>
  XML
end

.toggle_button(pagestyle) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/asciidoctor/html/book_template.rb', line 67

def self.toggle_button(pagestyle)
  states = {
    single: "single page view",
    multi: "multipage view",
    presentation: "presentation view (ESC to exit)"
  }
  alternate_states = states.map do |key, value|
    <<~HTML
      <li>
      <a class="dropdown-item#{" active" if pagestyle == key}" href="#page" data-viewmode="#{key}">#{value}</a>
      </li>
    HTML
  end
  <<~HTML
    <div class="layout-toggle">
      <div class="dropdown">
        <button class="btn btn-link dropdown-toggle" id="btn-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
          #{states[pagestyle]}
        </button>
        <ul id="viewmode-actions" class="dropdown-menu">
          #{alternate_states.join "\n"}
        </ul>
      </div>
    </div>
  HTML
end