<commit_before>(in-package #:cl-markdown-test)

(defvar *errors* nil)
(defvar *all-wells* nil)
(defvar *data* nil
  "What a hack! Shoot me")

(defparameter *test-source-directory* 
  (system-relative-path
   'cl-markdown 
   (make-pathname :directory ";unit-tests;markdown-tests;")))

(defparameter *test-output-directory*
  (system-relative-path
   'cl-markdown 
   (make-pathname :directory ";website;output;comparison-tests;")))

(defun compare-markdown-and-cl-markdown (basename)
  (cl-markdown-and-tidy basename)
  (markdown-and-tidy basename)
  (create-comparison-file basename))   

(defun create-main-comparison-page ()
  (let ((output (make-pathname :type "html"
                               :name "index"
                               :defaults *test-output-directory*)))
    (ensure-directories-exist output)
    (with-new-file (s output)
      (lml2:html-stream 
       s
       (lml2:html
        (:head (:title "Index | CL-Markdown / Markdown Comparison")
               ((:link :rel "stylesheet" :href "style.css")))
        (:body
         ((:div :id "contents")
          (:P 
           "Below are the results of running "
           ((:A :href "http://www.common-lisp.net/project/cl-markdown") "CL-Markdown")
           " and the Perl " ((:a :href "http://www.daringfireball.net/markdown") "Markdown") 
           " script on the same input. You'll see that the current version of CL-Markdown performs OK on a few 
documents, very poorly on others and not at all on some.")
          (:P "Sometimes, CL-Markdown produces invalid HTML. Most browsers will still display the output but "
              ((:A :href "tidy") "Tidy") " reports errors and produces no output. This will show up as a blank section on the comparison page")
          (:P 
           "This will be updated regularly. The most recent update was "
           (lml2:lml-princ (format-date "%e %B %Y" (get-universal-time))))

          (:H2 "Comparison Tests")
          
          (iterate-elements 
           (directory 
            (make-pathname :name :wild :type "text" :defaults *test-source-directory*))
           (lambda (file)
             (bind ((entry-file (comparison-file-name (pathname-name file)))
                    (entry (namestring (make-pathname :name (pathname-name entry-file)
                                                      :type "html")))
                    (data (find (pathname-name file) *data*
                                :test #'string-equal :key #'car))
                    ((nil replace insert delete) (or data (list nil nil nil nil))))
               (lml2:html
                ((:span :class 
                        (cond ((find (pathname-name file) *errors* :test #'string-equal)
                               "index-entry error")
                              ((find (pathname-name file) *all-wells* :test #'string-equal)
                               "index-entry good")
                              (t "index-entry")))
                 ((:a :href entry) (lml2:lml-princ entry)
                    (unless (and (zerop replace) (zerop delete) (zerop insert))
                      (lml2:lml-format " (~D, ~D, ~D)" replace delete insert))))))))
          
          ((:div :id "notes") 
           (:P "Files with this " ((:span :class "error") "color") " had Lisp errors during the run. "
               "Files with this " ((:span :class "good") "color") " had no differences from Markdown output during the run."
               "The numbers in parentheses represent the number of replacements, inserts, and deletes that occurred during the diff."))
          
          ((:div :id "footer") "end 'o page"))))))))

(defun compare-all ()
  (setf *errors* nil
        *all-wells* nil)
  (iterate-elements 
   (directory (make-pathname :name :wild :type "text" :defaults *test-source-directory*))
   (lambda (file)
     (handler-case
       (compare-markdown-and-cl-markdown (pathname-name file))
       (error (c) 
              (push (pathname-name file) *errors*)
              (create-error-file (pathname-name file) c)))))
  (create-main-comparison-page))

(defun cl-markdown-and-tidy (basename)
  (let* ((inpath (make-pathname :type "text"
                                :name basename 
                                :defaults *test-source-directory*))
         (output (make-pathname :type "html"
                                :name basename
                                :defaults *test-source-directory*)))
    (markdown inpath :format :html :stream output)
    (tidy basename "html" "xxxx")
    output))

(defun create-error-file (basename condition)
  (let ((output (comparison-file-name basename)))
    (ensure-directories-exist output)
    (with-new-file (s output)
      (lml2:html-stream 
       s<commit_msg>Comparison tests now copies CSS file automagically<commit_after>(in-package #:cl-markdown-test)

(defvar *errors* nil)
(defvar *all-wells* nil)
(defvar *data* nil
  "What a hack! Shoot me")

(defparameter *test-source-directory* 
  (system-relative-path
   'cl-markdown 
   (make-pathname :directory ";unit-tests;markdown-tests;")))

(defparameter *test-output-directory*
  (system-relative-path
   'cl-markdown 
   (make-pathname :directory ";website;output;comparison-tests;")))

(defun compare-markdown-and-cl-markdown (basename)
  (cl-markdown-and-tidy basename)
  (markdown-and-tidy basename)
  (create-comparison-file basename))   

(defun create-main-comparison-page ()
  (let ((output (make-pathname :type "html"
                               :name "index"
                               :defaults *test-output-directory*)))
    (ensure-directories-exist output)
    (with-new-file (s output)
      (lml2:html-stream 
       s
       (lml2:html
        (:head (:title "Index | CL-Markdown / Markdown Comparison")
               ((:link :rel "stylesheet" :href "style.css")))
        (:body
         ((:div :id "contents")
          (:P 
           "Below are the results of running "
           ((:A :href "http://www.common-lisp.net/project/cl-markdown") "CL-Markdown")
           " and the Perl " ((:a :href "http://www.daringfireball.net/markdown") "Markdown") 
           " script on the same input. You'll see that the current version of CL-Markdown performs OK on a few 
documents, very poorly on others and not at all on some.")
          (:P "Sometimes, CL-Markdown produces invalid HTML. Most browsers will still display the output but "
              ((:A :href "tidy") "Tidy") " reports errors and produces no output. This will show up as a blank section on the comparison page")
          (:P 
           "This will be updated regularly. The most recent update was "
           (lml2:lml-princ (format-date "%e %B %Y" (get-universal-time))))

          (:H2 "Comparison Tests")
          
          (iterate-elements 
           (directory 
            (make-pathname :name :wild :type "text" :defaults *test-source-directory*))
           (lambda (file)
             (bind ((entry-file (comparison-file-name (pathname-name file)))
                    (entry (namestring (make-pathname :name (pathname-name entry-file)
                                                      :type "html")))
                    (data (find (pathname-name file) *data*
                                :test #'string-equal :key #'car))
                    ((nil replace insert delete) (or data (list nil nil nil nil))))
               (lml2:html
                ((:span :class 
                        (cond ((find (pathname-name file) *errors* :test #'string-equal)
                               "index-entry error")
                              ((find (pathname-name file) *all-wells* :test #'string-equal)
                               "index-entry good")
                              (t "index-entry")))
                 ((:a :href entry) (lml2:lml-princ entry)
                    (unless (and (zerop replace) (zerop delete) (zerop insert))
                      (lml2:lml-format " (~D, ~D, ~D)" replace delete insert))))))))
          
          ((:div :id "notes") 
           (:P "Files with this " ((:span :class "error") "color") " had Lisp errors during the run. "
               "Files with this " ((:span :class "good") "color") " had no differences from Markdown output during the run."
               "The numbers in parentheses represent the number of replacements, inserts, and deletes that occurred during the diff."))
          
          ((:div :id "footer") "end 'o page"))))))))

(defun compare-all ()
  (setf *errors* nil
        *all-wells* nil)
  (iterate-elements 
   (directory (make-pathname :name :wild :type "text" :defaults *test-source-directory*))
   (lambda (file)
     (handler-case
       (compare-markdown-and-cl-markdown (pathname-name file))
       (error (c) 
              (push (pathname-name file) *errors*)
              (create-error-file (pathname-name file) c)))))
  (create-main-comparison-page)
  (copy-file (make-pathname :type "css"
                            :name "style" 
                            :defaults *test-source-directory*)
             (make-pathname :type "css"
                            :name "style"
                            :defaults *test-output-directory*)
             :if-exists :supersede))

(defun cl-markdown-and-tidy (basename)
  (let* ((inpath (make-pathname :type "text"
                                :name basename 
                                :defaults *test-source-directory*))
         (output (make-pathname :type "html"
                                :name basename
                                :defaults *test-source-directory*)))
    (markdown inpath :format :html :stream output)
    (tidy basename "html" "xxxx")
    output))

(defun create-error-file (basename condition)
  (let ((output (comparison-file-name basename)))
    (ensure-directories-exist output)
    (with-new-file (s output)
      (lml2:html-stream 
       s
{'fraction_non_alphanumeric': 0.13045234248788368, 'fraction_numerical': 0.0016155088852988692, 'mean_word_length': 1.8970459198596081, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '211666', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2765, 'n_words': 816}