<commit_before>; Copyright 2009, 2010, 2011 <NAME>
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;   http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
; implied. See the License for the specific language governing permissions
; and limitations under the License.

(ns
  cascade.internal.viewbuilder
  "Form parser for the template DSL"
  (:use 
    [clojure [pprint :only (pprint)]]
    [clojure.algo monads]
    [cascade dom fail]
    [cascade.internal parser]))

(defn convert-render-result
  [any]
  "Checks the result of invoking a rendering function (or evaluating a symbol), to ensure that only
   acceptible values are returned."
  (cond
    (dom-node? any) any
    (string? any) (text-node any)
    (number? any) (raw-node (str any))
    true (throw (RuntimeException.
      (format "A rendering function returned %s. Rendering functions should return nil, a string, a seq of DOM nodes, or a single DOM node."
    (pr-str any))))))

(defn combine
  "Given the results of rendering (where each step provides a render result), combine the results
into a single sequence of DOM nodes. Each of the render results should be a DOM node,
or a collection of DOM nodes (or a nested collection of DOM nodes, etc.). Strings are also allowed,
which are converted into :text DOM nodes."
  [& render-results]
  (loop [output (transient [])
         queue render-results]
    (let [current (first queue)
          remainder (next queue)]
      (cond
        (nil? current) (if (empty? remainder) (persistent! output) (recur output remainder))
        (sequential? current) (recur output (concat current remainder))
        :otherwise (recur (conj! output (convert-render-result current)) remainder)))))

(with-monad parser-m
  (declare parse-embedded-template)

  (def parse-text
    (domonad [text match-string]
      ; The encode-string occurs just once, at macro expansion time
      `(raw-node ~(encode-string text))))

  (def parse-name<commit_msg>Improve exception message about invalid return value from rendering function<commit_after>; Copyright 2009, 2010, 2011 <NAME>
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;   http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
; implied. See the License for the specific language governing permissions
; and limitations under the License.

(ns
  cascade.internal.viewbuilder
  "Form parser for the template DSL"
  (:use 
    [clojure [pprint :only (pprint)]]
    [clojure.algo monads]
    [cascade dom fail]
    [cascade.internal parser]))

(defn convert-render-result
  [any]
  "Checks the result of invoking a rendering function (or evaluating a symbol), to ensure that only
   acceptible values are returned."
  (cond
    (dom-node? any) any
    (string? any) (text-node any)
    (number? any) (raw-node (str any))
    true (throw (RuntimeException.
      (format "A rendering function returned %s. Rendering functions should return nil, a string, a number, a DOM node or a seq of such values."
    (pr-str any))))))

(defn combine
  "Given the results of rendering (where each step provides a render result), combine the results
into a single sequence of DOM nodes. Each of the render results should be a DOM node,
or a collection of DOM nodes (or a nested collection of DOM nodes, etc.). Strings are also allowed,
which are converted into :text DOM nodes."
  [& render-results]
  (loop [output (transient [])
         queue render-results]
    (let [current (first queue)
          remainder (next queue)]
      (cond
        (nil? current) (if (empty? remainder) (persistent! output) (recur output remainder))
        (sequential? current) (recur output (concat current remainder))
        :otherwise (recur (conj! output (convert-render-result current)) remainder)))))

(with-monad parser-m
  (declare parse-embedded-template)

  (def parse-text
    (domonad [text match-string]
      ; The encode-string occurs just once, at macro expansion time
      `(raw-node ~(encode-string text))))

  (def parse-name
{'fraction_non_alphanumeric': 0.09309374323446633, 'fraction_numerical': 0.006927906473262611, 'mean_word_length': 3.64321608040201, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1079506', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1269, 'n_words': 585}