Factor parse-initarg out of parse-metadata.

This commit is contained in:
Brit Butler 2014-09-22 12:56:00 -04:00
parent 33e81104b8
commit f474db77b2
2 changed files with 11 additions and 10 deletions

View file

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

View file

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