Quellcode durchsuchen

Factor parse-initarg out of parse-metadata.

Brit Butler vor 10 Jahren
Ursprung
Commit
f474db77b2
2 geänderte Dateien mit 11 neuen und 10 gelöschten Zeilen
  1. 10 10
      src/content.lisp
  2. 1 0
      src/packages.lisp

+ 10 - 10
src/content.lisp

@@ -48,22 +48,22 @@
48 48
     (when (stringp tags)
49 49
       (setf tags (mapcar #'make-tag (cl-ppcre:split "," tags))))))
50 50
 
51
+(defun parse-initarg (line)
52
+  "Given a metadata header, LINE, parse an initarg name/value pair from it."
53
+  (let ((name (string-upcase (subseq line 0 (position #\: line))))
54
+        (match (nth-value 1 (scan-to-strings "[a-zA-Z]+:\\s+(.*)" line))))
55
+    (when match
56
+      (list (make-keyword name) (aref match 0)))))
57
+
51 58
 (defun parse-metadata (stream)
52 59
   "Given a STREAM, parse metadata from it or signal an appropriate condition."
53
-  (labels ((get-next-line (input)
54
-             (string-trim '(#\Space #\Newline #\Tab) (read-line input nil)))
55
-           (parse-value (str)
56
-             (nth-value 1 (cl-ppcre:scan-to-strings "[a-zA-Z]+:\\s+(.*)" str)))
57
-           (parse-initarg-name (line)
58
-             (make-keyword (string-upcase (subseq line 0 (position #\: line)))))
59
-           (extract-initarg (line)
60
-             (list (parse-initarg-name line) (aref (parse-value line) 0))))
60
+  (flet ((get-next-line (input)
61
+           (string-trim '(#\Space #\Newline #\Tab) (read-line input nil))))
61 62
     (unless (string= (get-next-line stream) (separator *config*))
62 63
       (error "The file lacks the expected header: ~a" (separator *config*)))
63 64
     (loop for line = (get-next-line stream)
64 65
        until (string= line (separator *config*))
65
-       when (parse-value line)
66
-       appending (extract-initarg line))))
66
+       appending (parse-initarg line))))
67 67
 
68 68
 (defun read-content (file)
69 69
   "Returns a plist of metadata from FILE with :text holding the content."

+ 1 - 0
src/packages.lisp

@@ -5,6 +5,7 @@
5 5
                             #:make-keyword
6 6
                             #:mappend)
7 7
   (:import-from :cl-fad #:file-exists-p)
8
+  (:import-from :cl-ppcre #:scan-to-strings)
8 9
   (:import-from :closure-template #:compile-template)
9 10
   (:import-from :local-time #:format-rfc1123-timestring)
10 11
   (:import-from :uiop #:getcwd