File: pdf-index-customizer.rb

package info (click to toggle)
openxr-sdk-source 1.0.14~dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,564 kB
  • sloc: python: 16,103; cpp: 12,052; ansic: 8,813; xml: 3,480; sh: 410; makefile: 338; ruby: 247
file content (44 lines) | stat: -rw-r--r-- 1,613 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
# Copyright (c) 2020-2021, The Khronos Group Inc.
#
# SPDX-License-Identifier: Apache-2.0

# This file customizes the way that asciidoctor-pdf makes the index,
# so it's not all uselessly sorted under "X" since all the terms start with "XR"

# see https://github.com/asciidoctor/asciidoctor-pdf#how-index-terms-are-grouped-and-sorted

require 'asciidoctor-pdf'

# Normalizes an OpenXR entity name to just the part that matters for ordering.
def strip_prefix(name)
  name.sub /^[xX][rR](_?)/, ""
end

module Asciidoctor::PDF
  # Override how the "Category" (first letter heading) is computed
  # Docs and source for original version:
  # https://www.rubydoc.info/github/asciidoctor/asciidoctor-pdf/Asciidoctor/Pdf/IndexCatalog#store_primary_term-instance_method
  IndexCatalog.prepend(
    ::Module.new do
      def store_primary_term(name, dest = nil)
        store_dest dest if dest
        # After stripping the prefix (if any) do a multibyte-uppercase
        # and grab the first character as the category
        category = uppercase_mb(strip_prefix(name)).chr
        (init_category category).store_term name, dest
      end
    end
  )

  # Override how index terms are sorted: they ignore their prefix.
  # Docs and source for original version:
  # https://www.rubydoc.info/github/asciidoctor/asciidoctor-pdf/Asciidoctor/Pdf/IndexTermGroup#%3C=%3E-instance_method
  IndexTermGroup.prepend (
      ::Module.new do
        def <=>(other)
          this = strip_prefix(@name)
          that = strip_prefix(other.name)
          (val = this.casecmp that) == 0 ? this <=> that : val
        end
      end)
end