Browse code
Reorganize format.lisp
fiddlerwoaroof authored on 30/01/2017 20:57:46
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -1,19 +1,17 @@ |
1 | 1 |
(defpackage :alimenta.format |
2 |
- (:use :cl :alexandria :serapeum :fw.lu)) |
|
2 |
+ (:use :cl :alexandria :serapeum :fw.lu) |
|
3 |
+ (:export format-document format-title format-link format-paragraph)) |
|
3 | 4 |
(in-package :alimenta.format) |
4 | 5 |
|
5 | 6 |
(defclass document-formatter () |
6 | 7 |
()) |
7 | 8 |
|
8 |
-(defclass indent-formatter (document-formatter) |
|
9 |
- ((%level :initarg :level :accessor level :initform 0))) |
|
10 |
- |
|
11 |
-(defclass html-formatter (document-formatter) |
|
12 |
- ((%level :initarg :level :accessor level :initform 0))) |
|
13 |
- |
|
9 |
+;;; Formatting protocol |
|
10 |
+;; Generic entrypoint |
|
14 | 11 |
(defgeneric format-document (formatter stream document) |
15 | 12 |
(:documentation "Format a document with the given formatter to the given stream")) |
16 | 13 |
|
14 |
+;; Semantic formatters |
|
17 | 15 |
(defgeneric format-title (formatter title) |
18 | 16 |
(:documentation "Format a title according to FORMATTER")) |
19 | 17 |
|
... | ... |
@@ -23,24 +21,7 @@ |
23 | 21 |
(defgeneric format-paragraph (formatter paragraph) |
24 | 22 |
(:documentation "Format a paragraph according to FORMATTER")) |
25 | 23 |
|
26 |
-(defmethod format-title ((formatter indent-formatter) (title string)) |
|
27 |
- (format nil "+ Title: ~a" title)) |
|
28 |
- |
|
29 |
-(defmethod format-title ((formatter html-formatter) (title string)) |
|
30 |
- (format nil "<h~d>~a~2:*</h~d>" (1+ (level formatter)) title)) |
|
31 |
- |
|
32 |
-(defmethod format-link ((formatter indent-formatter) (link string)) |
|
33 |
- (format nil " Link: ~a" link)) |
|
34 |
- |
|
35 |
-(defmethod format-link ((formatter html-formatter) (link string)) |
|
36 |
- (format nil "<a href=\"~a\">~:*~a</a>" link)) |
|
37 |
- |
|
38 |
-(defmethod format-paragraph ((formatter indent-formatter) (paragraph list)) |
|
39 |
- (format nil "~{ ~a~}" paragraph)) |
|
40 |
- |
|
41 |
-(defmethod format-paragraph ((formatter html-formatter) (paragraph list)) |
|
42 |
- (format nil "~%~{<p>~a</p>~}" paragraph)) |
|
43 |
- |
|
24 |
+;;; Make alimenta's classes formattable |
|
44 | 25 |
|
45 | 26 |
(defmethod format-document (formatter stream (document alimenta::feed-entity)) |
46 | 27 |
(format stream "~&~v,4@t~a~%~v,4@t~a~%" |
... | ... |
@@ -55,12 +36,39 @@ |
55 | 36 |
(format stream "~&~v,4@t~a~2&" |
56 | 37 |
(level formatter) (format-paragraph formatter (map 'list #'identity paragraphs))))) |
57 | 38 |
|
39 |
+ |
|
40 |
+;;; Define some output formats |
|
41 |
+ |
|
42 |
+(defclass indent-formatter (document-formatter) |
|
43 |
+ ((%level :initarg :level :accessor level :initform 0))) |
|
44 |
+ |
|
45 |
+(defmethod format-title ((formatter indent-formatter) (title string)) |
|
46 |
+ (format nil "+ Title: ~a" title)) |
|
47 |
+ |
|
48 |
+(defmethod format-link ((formatter indent-formatter) (link string)) |
|
49 |
+ (format nil " Link: ~a" link)) |
|
50 |
+ |
|
51 |
+(defmethod format-paragraph ((formatter indent-formatter) (paragraph list)) |
|
52 |
+ (format nil "~{ ~a~}" paragraph)) |
|
53 |
+ |
|
58 | 54 |
(defmethod format-document ((formatter indent-formatter) stream (document alimenta:feed)) |
59 | 55 |
(call-next-method) |
60 | 56 |
(incf (level formatter)) |
61 | 57 |
(for:for ((item over document)) |
62 | 58 |
(format-document formatter stream item))) |
63 | 59 |
|
60 |
+(defclass html-formatter (document-formatter) |
|
61 |
+ ((%level :initarg :level :accessor level :initform 0))) |
|
62 |
+ |
|
63 |
+(defmethod format-title ((formatter html-formatter) (title string)) |
|
64 |
+ (format nil "<h~d>~a~2:*</h~d>" (1+ (level formatter)) title)) |
|
65 |
+ |
|
66 |
+(defmethod format-link ((formatter html-formatter) (link string)) |
|
67 |
+ (format nil "<a href=\"~a\">~:*~a</a>" link)) |
|
68 |
+ |
|
69 |
+(defmethod format-paragraph ((formatter html-formatter) (paragraph list)) |
|
70 |
+ (format nil "~%~{<p>~a</p>~}" paragraph)) |
|
71 |
+ |
|
64 | 72 |
(defmethod format-document ((formatter html-formatter) stream (document alimenta:feed)) |
65 | 73 |
(let ((ostream (or stream (make-string-output-stream)))) |
66 | 74 |
(unwind-protect |