[cl-who-devel] Generating html tree with dynamic structure
Ala'a (cmo-0)
amalawi at gmail.com
Mon Nov 23 19:26:06 UTC 2009
this is a my first shot. I do not know what is an s-tree, thus the
implementation of 'text-style'
(defmethod p-render-html ((s-tree tree) (p text-piece) stream)
(with-html-output (stream nil :prologue nil)
(let ((text (text-of p)))
(if (styled-p text-piece)
(styled-str text )
(str text)))))
(defun styled-p (text-piece)
(if (style-of text-piece) t nil))
(defun styled-str (text)
(dolist (style (text-styles text))
(setf text (apply-style style text)))
text) ;; or you can use recursive style
;; this is only for light testing
(defun text-styles (text)
(list 'italic 'bold))
;; you may change the internals to a macro (aka defstyle ) based for
example on a hastable of lambdas
;; rather than using and maintaining/updating ecase form manually
(defun apply-style (style text)
(with-output-to-string (out)
(with-html-output (out nil :prologue nil)
(ecase style
('italic (htm (:italic (str text))))
('bold (htm (:strong (str text))))))))
Regards,
Ala'a (cmo-0)
On Mon, Nov 23, 2009 at 8:37 PM, Dmitry V'yal <akamaus at gmail.com> wrote:
> Hello anyone,
>
> I have a simple problem, but I can't find an elegant solution.
>
> Basically I have a message and two flags, the first one signals message
> should be rendered in <strong> tag and the second - in <italic>. So
> there are four possibilities.
>
> Here is the actual code:
>
> (defmethod p-render-html ((s-tree tree) (p text-piece) s)
> (with-html-output (s nil :prologue nil)
> (let ((style (style-of p))
> (txt (text-of p)))
> (if style
> (if (tree-find-prop s-tree style #'bold)
> (htm (:strong (if (tree-find-prop s-tree style #'italic)
> (htm (:italic (str txt)))
> (str txt))))
> (if (tree-find-prop s-tree style #'italic)
> (htm (:italic (str txt)))
> (str txt)))
> (str txt)))))
>
> I had to test for each case and it resulted in code duplication. Can it
> be avoided? What if I had 5 flags or so?
>
> _______________________________________________
> cl-who-devel site list
> cl-who-devel at common-lisp.net
> http://common-lisp.net/mailman/listinfo/cl-who-devel
>
--
It does not matter how fast your code is, if it does not work!
More information about the Cl-who-devel
mailing list