From 496c9f67fca0a973d33f6bda4eac722472b67336 Mon Sep 17 00:00:00 2001 From: Greg Look Date: Fri, 6 Mar 2015 19:57:52 -0800 Subject: [PATCH 1/3] Add :escape-types option. --- src/puget/printer.clj | 68 ++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 23 deletions(-) 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 From a154ccb2f21030807423b1da3d076bfedf06c053 Mon Sep 17 00:00:00 2001 From: Greg Look Date: Fri, 6 Mar 2015 20:10:34 -0800 Subject: [PATCH 2/3] Add test for :print-fallback and :escape-types. --- test/puget/printer_test.clj | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/puget/printer_test.clj b/test/puget/printer_test.clj index 1b30b97..1e4ee17 100644 --- a/test/puget/printer_test.clj +++ b/test/puget/printer_test.clj @@ -132,7 +132,10 @@ (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 + :escape-types #{'puget.printer_test.TestRecord}} + (is (= "#" (pprint-str usd))))))) (deftest metadata-printing From bd3c015eac7bfd44a3f919b9d11fabd94784f0be Mon Sep 17 00:00:00 2001 From: Greg Look Date: Sat, 7 Mar 2015 10:51:19 -0800 Subject: [PATCH 3/3] Improve test for type escaping and print fallback. --- test/puget/printer_test.clj | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/test/puget/printer_test.clj b/test/puget/printer_test.clj index 1e4ee17..a34a351 100644 --- a/test/puget/printer_test.clj +++ b/test/puget/printer_test.clj @@ -128,14 +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))) - (with-options {:print-fallback :print - :escape-types #{'puget.printer_test.TestRecord}} - (is (= "#" (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