diff --git a/src/puget/printer.clj b/src/puget/printer.clj index c905006..af7c78e 100644 --- a/src/puget/printer.clj +++ b/src/puget/printer.clj @@ -15,51 +15,66 @@ (def ^:dynamic *options* "Printer control options. - `:width` + #### General Rendering + + `:width`
Number of characters to try to wrap pretty-printed forms at. - `:sort-keys` + `:print-meta`
+ If true, metadata will be printed before values. If nil, defaults to the + value of `*print-meta*`. + + `:sort-keys`
Print maps and sets with ordered keys. Defaults to true, which will sort all collections. If a number, counted collections will be sorted up to the set size. Otherwise, collections are not sorted before printing. - `:strict` - If true, throw an exception if there is no canonical EDN representation for - a given value. This generally applies to any non-primitive value which does - not extend puget.data/TaggedValue and is not a built-in collection. - - `:map-delimiter` + `:map-delimiter`
The text placed between key-value pairs in a map. - `:map-coll-separator` + `:map-coll-separator`
The text placed between a map key and a collection value. The keyword :line will cause line breaks if the whole map does not fit on a single line. - `:print-fallback` - Takes a keyword argument specifying the desired string representation of - unknown documents. The keyword `:print` will fall back to using `pr-str` - rather than puget's default unknown-document representation. - `:print-meta` - If true, metadata will be printed before values. If nil, defaults to the - value of *print-meta*. + #### Type Handling + + `:print-fallback`
+ Keyword argument specifying how to format unknown values. The keyword + `:print` will fall back to using `pr-str` rather than the default + pretty-printed representation. - `:print-color` + `:escape-types`
+ A set of symbols naming classes which should *not* be pretty-printed. Instead, + they will be rendered as unknown values. This can be useful for types which + define their own `print-method`, are extremely large nested structures, or + which Puget otherwise has trouble rendering. + + `:strict`
+ If true, throw an exception if there is no canonical EDN representation for + a given value. This generally applies to any non-primitive value which does + not extend `ExtendedNotation` and is not a built-in collection. + + + #### Color Options + + `:print-color`
When true, ouptut colored text from print functions. - `:color-markup` + `:color-markup`
:ansi for ANSI color text (the default), :html-inline for inline-styled html, :html-classes to use the names of the keys in the :color-scheme map as class names for spans so styling can be specified via CSS. - `:color-scheme` + `:color-scheme`
Map of syntax element keywords to ANSI color codes." {:width 80 :sort-keys true :strict false :map-delimiter "," :map-coll-separator " " + :escape-types nil :print-fallback nil :print-meta nil :print-color false @@ -161,13 +176,20 @@ ;; ## Formatting Multimethod (defn- formatter-dispatch - "Dispatches the method to use for value formatting. Values which use extended + "Dispatches the method to use for value formatting. Any types in the + `:escape-types` set use the default formatter; values which use extended notation are rendered as tagged literals; others are dispatched on their `type`." [value] - (if (satisfies? data/ExtendedNotation value) - ::tagged-literal - (type value))) + (let [class-sym (some-> value class .getName symbol)] + (cond + (contains? (:escape-types *options*) class-sym) + :default + + (satisfies? data/ExtendedNotation value) + ::tagged-literal + + :else (type value)))) (defmulti format-doc diff --git a/test/puget/printer_test.clj b/test/puget/printer_test.clj index 1b30b97..a34a351 100644 --- a/test/puget/printer_test.clj +++ b/test/puget/printer_test.clj @@ -128,11 +128,33 @@ (is (= "#frobble/biznar\n[:foo :bar :baz]" (pprint-str tv))))) +(deftype ComplexValue [] + Object + (toString [_] "to-string")) + +(defmethod print-method ComplexValue + [this w] + (.write w "{{ complex value print }}")) + +(data/extend-tagged-str ComplexValue 'complex/val) + + (deftest default-formatting (testing "Unknown values" (let [usd (java.util.Currency/getInstance "USD")] (should-fail-when-strict usd) - (is (re-seq #"#" (pprint-str usd)))))) + (is (re-seq #"#" (pprint-str usd))) + (with-options {:print-fallback :print} + (is (= "#" (pprint-str usd)))))) + (testing "Escaped types" + (let [cv (ComplexValue.)] + (with-options {:escape-types nil} + (is (= "#complex/val \"to-string\"" (pprint-str cv)))) + (with-options {:escape-types #{'puget.printer_test.ComplexValue}} + (is (re-seq #"#" (pprint-str cv)))) + (with-options {:escape-types #{'puget.printer_test.ComplexValue} + :print-fallback :print} + (is (= "{{ complex value print }}" (pprint-str cv))))))) (deftest metadata-printing