Skip to content

Instantly share code, notes, and snippets.

@Cortys
Last active September 13, 2023 10:22
Show Gist options
  • Save Cortys/d861ce8701a4d4cba74c85f873214295 to your computer and use it in GitHub Desktop.
Save Cortys/d861ce8701a4d4cba74c85f873214295 to your computer and use it in GitHub Desktop.
This script generates a sequence of Ghostscript commands for adding ToC metadata to a PDF.
Contents 8
Preface to the Second Edition 5
Preface to the First Edition 6
Introduction 1 [11]
I. Categories, Functors, and Natural Transformations 7 [10]
1. Axioms for Categories 7
2. Categories 10
3. Functors 13
4. Natural Transformations 16
5. Monics, Epis, and Zeros 19
6. Foundations 21
7. Large Categories 24
8. Hom-Sets 27
II. Constructions on Categories 31
1. Duality 31
2. Contravariance and Opposites 33
3. Products of Categories 36
4. Functor Categories 40
5. The Category of All Categories 42
6. Comma Categories 45
7. Graphs and Free Categories 48
8. Quotient Categories 51
III. Universals and Limits 55 [9]
1. Universal Arrows 55
2. The Yoneda Lemma 59
3. Coproducts and Colimits 62
4. Products and Limits 68
5. Categories with Finite Products 72
6. Groups in Categories 75
7. Colimits of Representable Functors 76
IV. Adjoints 79
1. Adjunctions 79
2. Examples of Adjoints 86
3. Reflective Subcategories 90
4. Equivalence of Categories 92
5. Adjoints for Preorders 95
6. Cartesian Closed Categories 97
7. Transformations of Adjoints 99
8. Composition of Adjoints 103
9. Subsets and Characteristic Functions 105
10. Categories Like Sets 106
V. Limits 109
1. Creation of Limits 109
2. Limits by Products and Equalizers 112
3. Limits with Parameters 115
4. Preservation of Limits 116
5. Adjoints on Limits 118
6. Freyd's Adjoint Functor Theorem 120
7. Subobjects and Generators 126
8. The Special Adjoint Functor Theorem 128
9. Adjoints in Topology 132
VI. Monads and Algebras 137
1. Monads in a Category 137
2. Algebras for a Monad 139
3. The Comparison with Algebras 142
4. Words and Free Semigroups 144
5. Free Algebras for a Monad 147
6. Split Coequalizers 149
7. Beck's Theorem 151
8. Algebras Are T-Algebras 156
9. Compact Hausdorff Spaces 157
VII. Monoids 161 [8]
1. Monoidal Categories 161
2. Coherence 165
3. Monoids 170
4. Actions 174
5. The Simplicial Category 175
6. Monads and Homology 180
7. Closed Categories 184
8. Compactly Generated Spaces 185
9. Loops and Suspensions 188
VIII. Abelian Categories 191
1. Kernels and Cokernels 191
2. Additive Categories 194
3. Abelian Categories 198
4. Diagram Lemmas 202
IX. Special Limits 211 [7]
1. Filtered Limits 211
2. Interchange of Limits 214
3. Final Functors 217
4. Diagonal Naturality 218
5. Ends 222
6. Coends 226
7. Ends with Parameters 228
8. Iterated Ends and Limits 230
X. Kan Extensions 233
1. Adjoints and Limits 233
2. Weak Universality 235
3. The Kan Extension 236
4. Kan Extensions as Coends 240
5. Pointwise Kan Extensions 243
6. Density 245
7. All Concepts Are Kan Extensions 248
XI. Symmetry and Braiding in Monoidal Categories 251
1. Symmetric Monoidal Categories 251
2. Monoidal Functors 255
3. Strict Monoidal Categories 257
4. The Braid Groups Bn and the Braid Category 260
5. Braided Coherence 263
6. Perspectives 266
XII. Structures in Categories 267
1. Internal Categories 267
2. The Nerve of a Category 270
3. 2-Categories 272
4. Operations in 2-Categories 276
5. Single-Set Categories 279
6. Bicategories 281
7. Examples of Bicategories 283
8. Crossed Modules and Categories in Grp 285
Appendix. Foundations 289 [6]
Table of Standard Categories: Objects and Arrows 293 [5]
Table of Terminology 295 [4]
Bibliography 297 [3]
Index 303
(ns toc
(:require [clojure.string :as str]
[clojure.zip :as zip]))
(def toc-lines (->> "toc.txt"
slurp
str/split-lines))
(defn map-zipper
([] (map-zipper {}))
([root]
(zip/zipper (constantly true) :children
#(assoc %1 :children %2)
root)))
(def toc-tree (transduce (comp (remove empty?)
(map #(next (re-matches #"^(\t*)(.+?) (\d+)\s*(-?\[\d+\])?\s*$" %)))
(map (fn [[indent label page adjust]]
(let [indent (count indent)
label (str/trim label)
page (-> page str/trim parse-long)
adjust (some-> adjust (subs 1 (dec (count adjust))) parse-long)]
{:indent indent
:label label
:page page
:adjust adjust}))))
(fn reducer
([{:keys [loc]}]
(zip/root loc))
([{:keys [loc global-adjust]} {:keys [indent adjust] :as item}]
(let [tree-depth (count (zip/path loc))
tree (cond
(< tree-depth indent) (-> loc zip/down zip/rightmost)
(> tree-depth indent) (-> loc zip/up)
:else loc)
adjust (or adjust global-adjust)
item (update item :page + adjust)
item (if (zero? tree-depth)
(assoc item :expanded true)
item)]
{:global-adjust adjust
:loc (zip/append-child tree item)})))
{:global-adjust 0 :loc (map-zipper)} toc-lines))
(def toc-items
(loop [loc (zip/next (map-zipper toc-tree))
items []]
(if (zip/end? loc)
items
(recur (zip/next loc)
(conj items (update (zip/node loc) :children count))))))
(def gs (->> toc-items
(map (fn [{:keys [label page children expanded]}]
(str "[ /Title (" label ")\n"
" /Page " page "\n"
(when-not (zero? children)
(str " /Count "
(if expanded "" "-")
children "\n"))
" /OUT pdfmark\n")))
(str/join "\n")))
(spit "toc.gs" gs)
(comment
"gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf maclane1978.pdf toc.gs"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment