     (header :verso)
 
     [:fo:flow {:flow-name "xsl-region-body"}
-     (toc items layout/formats)
-     (mapcat #(format-sexp items %) layout/formats)]]])
+     (toc items)
+     (mapcat #(format-sexp items %) (order (keys items)))]]])
 
 (defn document [items & args]
   (-> items

  [:fo:block {:keep-with-next "always"}
   [:fo:marker {:marker-class-name running-header-class-name} title]])

(defn toc-entry [items fmt]
  (when (fmt items)
    [:fo:block {:text-align-last "justify"}
     [:fo:basic-link {:internal-destination (hash [fmt])}
      (str (layout/translations fmt) " ")
      [:fo:leader {:leader-pattern "dots"}]
      [:fo:page-number-citation {:ref-id (hash [fmt])}]]]))

(defn toc [items formats]
  [:fo:block {:line-height "150%"}
   (h1 :inhalt)
   (keep #(toc-entry items %) formats)])

(defn- sub-sub-toc-entry [items fmt genre subgenre]
  (when-let [items (subgenre items)]
    [:fo:block {:text-align-last "justify" :start-indent "1em"}
     [:fo:basic-link {:internal-destination (hash [fmt genre subgenre])}
      (str (layout/translations subgenre) " ")
      [:fo:leader {:leader-pattern "dots"}]
      [:fo:page-number-citation {:ref-id (hash [fmt genre subgenre])}]]]))

(defn- sub-toc-entry [items fmt genre]
  (when-let [items (genre items)]
    [:fo:block
     [:fo:block {:text-align-last "justify"}
      [:fo:basic-link {:internal-destination (hash [fmt genre])}
       (str (layout/translations genre) " ")
       [:fo:leader {:leader-pattern "dots"}]
       [:fo:page-number-citation {:ref-id (hash [fmt genre])}]]]
     (keep #(sub-sub-toc-entry items fmt genre %) layout/subgenres)]))

(defn sub-toc [items fmt]
  [:fo:block {:line-height "150%"}
   (keep #(sub-toc-entry items fmt %)
         (if (= fmt :braille) layout/braille-genres layout/genres))])

(def wrap (layout/wrapper ""))


(ns catalog.layout.fop
  (:require [catalog.layout.common :as layout]
            [clojure
             [set :as set]
             [string :as string]]
            [clojure.data.xml :as xml]
            [clojure.java.io :as io])
  (:import java.io.StringReader
           javax.xml.transform.sax.SAXResult
           javax.xml.transform.stream.StreamSource
