From: Dmitriy Ivanov
Subject: Code for converting forum pseudo-tags to HTML
Date:
Message-ID: <g1g8id$1l9p$1@news.aha.ru>
Hello folks,
Is anybody aware of an open lisp code for converting "typical" forum
messages to HTML?
Message text can include pseudo-tags like [b]...[/b], [url=address]...[/url]
and the like. Additionally, empty lines usually indicate a new paragraph and
so on.
--
Sincerely,
Dmitriy Ivanov
lisp.ystok.ru
"Dmitriy Ivanov" <············@M_aha.ru> writes:
> Hello folks,
>
> Is anybody aware of an open lisp code for converting "typical" forum
> messages to HTML?
>
> Message text can include pseudo-tags like [b]...[/b], [url=address]...[/url]
> and the like. Additionally, empty lines usually indicate a new paragraph and
> so on.
Hm isn't that wiki markup? So a wiki for Common Lisp should contain
that code
Regards
Friedrich
--
Please remove just-for-news- to reply via e-mail.
On Wed, 28 May 2008 14:28:57 +0200, Friedrich Dominicus
<···················@q-software-solutions.de> tried to confuse everyone with
this message:
>"Dmitriy Ivanov" <············@M_aha.ru> writes:
>
>> Hello folks,
>>
>> Is anybody aware of an open lisp code for converting "typical" forum
>> messages to HTML?
>>
>> Message text can include pseudo-tags like [b]...[/b], [url=address]...[/url]
>> and the like. Additionally, empty lines usually indicate a new paragraph and
>> so on.
>Hm isn't that wiki markup? So a wiki for Common Lisp should contain
>that code
>
No, it's message boards markup. I'm currently implementing one in Common Lisp.
Some code (depends on cl-who, cl-ppcre and trivial functions):
(defun parse-token (token)
"foo -> foo, [foo bar] -> (foo bar), [[foo] -> [foo "
(cond
((or (zerop (length token))
(not (and (char= (char token 0) #\[)
(char= (char token (1- (length token))) #\]))))
token)
((char= (char token 1) #\[)
(subseq token 1 (- (length token) 1)))
(t (split "\\s+" (subseq token 1 (- (length token) 1))))))
(defun preparse-post (text)
"Find all tokens of the form [...] in the text and construct a list
of them and intermediate text"
(mapcar #'parse-token (split "(\\[[^[]*])" text :with-registers-p t)))
(defun find-matching-token (list)
"Return a list of the form (((token . data) text-in-token-scope)
remaining-text)"
(let ((token-name (caar list)))
(loop for (token . rest) on list
until (and (listp token) (string-equal token-name (subseq (car token)
1)))
collecting token into token-scope
finally (return (cons token-scope rest)))))
(defun parse-post (text)
"Produces a parse tree out of the text"
(labels ((parse (list)
(cond
((endp list) nil)
((listp (car list))
(destructuring-bind ((token &rest inside) &rest outside)
(find-matching-token list)
(cons (cons token (parse inside)) (parse outside))))
(t (cons (car list) (parse (cdr list)))))))
(parse (preparse-post text))))
(defun format-node (node stream)
(flet ((html-wrap (name)
(format stream "<~a>" name)
(format-tree (cdr node) stream)
(format stream "</~a>" name)))
(cond
((stringp node)
(replace-line-breaks (escape-string node) stream))
(t (destructuring-bind ((name &rest data) &rest tree) node
(cond
((string-equal name "b")
(html-wrap "b"))
((string-equal name "i")
(html-wrap "i"))
((string-equal name "url")
(let ((url (if data
(car data)
(if (stringp (car tree))
(car tree)
"#"))))
(format stream "<a href=\"~a\">" (escape-string url)))
(format-tree tree stream)
(princ "</a>" stream))
(t
(format stream "[~a]" (escape-string name))
(format-tree tree stream))))))))
(defun format-tree (tree stream)
(dolist (token tree) (format-node token stream)))
(defun process-post (str)
(format-tree (parse-post str) *standard-output*))
--
|Don't believe this - you're not worthless ,gr---------.ru
|It's us against millions and we can't take them all... | ue il |
|But we can take them on! | @ma |
| (A Wilhelm Scream - The Rip) |______________|
On Wed, 28 May 2008 14:28:57 +0200, Friedrich Dominicus
<···················@q-software-solutions.de> tried to confuse everyone with
this message:
>"Dmitriy Ivanov" <············@M_aha.ru> writes:
>
>> Hello folks,
>>
>> Is anybody aware of an open lisp code for converting "typical" forum
>> messages to HTML?
>>
>> Message text can include pseudo-tags like [b]...[/b], [url=address]...[/url]
>> and the like. Additionally, empty lines usually indicate a new paragraph and
>> so on.
>Hm isn't that wiki markup? So a wiki for Common Lisp should contain
>that code
>
No, it's message boards markup. I'm currently implementing one in Common Lisp.
Some code (depends on cl-who, cl-ppcre and trivial functions):
(defun parse-token (token)
"foo -> foo, [foo bar] -> (foo bar), [[foo] -> [foo "
(cond
((or (zerop (length token))
(not (and (char= (char token 0) #\[)
(char= (char token (1- (length token))) #\]))))
token)
((char= (char token 1) #\[)
(subseq token 1 (- (length token) 1)))
(t (split "\\s+" (subseq token 1 (- (length token) 1))))))
(defun preparse-post (text)
"Find all tokens of the form [...] in the text and construct a list
of them and intermediate text"
(mapcar #'parse-token (split "(\\[[^[]*])" text :with-registers-p t)))
(defun find-matching-token (list)
"Return a list of the form (((token . data) text-in-token-scope)
remaining-text)"
(let ((token-name (caar list)))
(loop for (token . rest) on list
until (and (listp token) (string-equal token-name (subseq (car token)
1)))
collecting token into token-scope
finally (return (cons token-scope rest)))))
(defun parse-post (text)
"Produces a parse tree out of the text"
(labels ((parse (list)
(cond
((endp list) nil)
((listp (car list))
(destructuring-bind ((token &rest inside) &rest outside)
(find-matching-token list)
(cons (cons token (parse inside)) (parse outside))))
(t (cons (car list) (parse (cdr list)))))))
(parse (preparse-post text))))
(defun format-node (node stream)
(flet ((html-wrap (name)
(format stream "<~a>" name)
(format-tree (cdr node) stream)
(format stream "</~a>" name)))
(cond
((stringp node)
(replace-line-breaks (escape-string node) stream))
(t (destructuring-bind ((name &rest data) &rest tree) node
(cond
((string-equal name "b")
(html-wrap "b"))
((string-equal name "i")
(html-wrap "i"))
((string-equal name "url")
(let ((url (if data
(car data)
(if (stringp (car tree))
(car tree)
"#"))))
(format stream "<a href=\"~a\">" (escape-string url)))
(format-tree tree stream)
(princ "</a>" stream))
(t
(format stream "[~a]" (escape-string name))
(format-tree tree stream))))))))
(defun format-tree (tree stream)
(dolist (token tree) (format-node token stream)))
(defun process-post (str)
(format-tree (parse-post str) *standard-output*))
--
|Don't believe this - you're not worthless ,gr---------.ru
|It's us against millions and we can't take them all... | ue il |
|But we can take them on! | @ma |
| (A Wilhelm Scream - The Rip) |______________|
On Wed, 28 May 2008 14:28:57 +0200, Friedrich Dominicus
<···················@q-software-solutions.de> tried to confuse everyone with
this message:
>"Dmitriy Ivanov" <············@M_aha.ru> writes:
>
>> Hello folks,
>>
>> Is anybody aware of an open lisp code for converting "typical" forum
>> messages to HTML?
>>
>> Message text can include pseudo-tags like [b]...[/b], [url=address]...[/url]
>> and the like. Additionally, empty lines usually indicate a new paragraph and
>> so on.
>Hm isn't that wiki markup? So a wiki for Common Lisp should contain
>that code
>
No, it's message boards markup. I'm currently implementing one in Common Lisp.
Some code (depends on cl-who, cl-ppcre and trivial functions):
(defun parse-token (token)
"foo -> foo, [foo bar] -> (foo bar), [[foo] -> [foo "
(cond
((or (zerop (length token))
(not (and (char= (char token 0) #\[)
(char= (char token (1- (length token))) #\]))))
token)
((char= (char token 1) #\[)
(subseq token 1 (- (length token) 1)))
(t (split "\\s+" (subseq token 1 (- (length token) 1))))))
(defun preparse-post (text)
"Find all tokens of the form [...] in the text and construct a list
of them and intermediate text"
(mapcar #'parse-token (split "(\\[[^[]*])" text :with-registers-p t)))
(defun find-matching-token (list)
"Return a list of the form (((token . data) text-in-token-scope)
remaining-text)"
(let ((token-name (caar list)))
(loop for (token . rest) on list
until (and (listp token) (string-equal token-name (subseq (car token)
1)))
collecting token into token-scope
finally (return (cons token-scope rest)))))
(defun parse-post (text)
"Produces a parse tree out of the text"
(labels ((parse (list)
(cond
((endp list) nil)
((listp (car list))
(destructuring-bind ((token &rest inside) &rest outside)
(find-matching-token list)
(cons (cons token (parse inside)) (parse outside))))
(t (cons (car list) (parse (cdr list)))))))
(parse (preparse-post text))))
(defun format-node (node stream)
(flet ((html-wrap (name)
(format stream "<~a>" name)
(format-tree (cdr node) stream)
(format stream "</~a>" name)))
(cond
((stringp node)
(replace-line-breaks (escape-string node) stream))
(t (destructuring-bind ((name &rest data) &rest tree) node
(cond
((string-equal name "b")
(html-wrap "b"))
((string-equal name "i")
(html-wrap "i"))
((string-equal name "url")
(let ((url (if data
(car data)
(if (stringp (car tree))
(car tree)
"#"))))
(format stream "<a href=\"~a\">" (escape-string url)))
(format-tree tree stream)
(princ "</a>" stream))
(t
(format stream "[~a]" (escape-string name))
(format-tree tree stream))))))))
(defun format-tree (tree stream)
(dolist (token tree) (format-node token stream)))
(defun process-post (str)
(format-tree (parse-post str) *standard-output*))
--
|Don't believe this - you're not worthless ,gr---------.ru
|It's us against millions and we can't take them all... | ue il |
|But we can take them on! | @ma |
| (A Wilhelm Scream - The Rip) |______________|
In article <·················@news.motzarella.org>,
····@mail.ru (Timofei Shatrov) wrote:
> On Wed, 28 May 2008 14:28:57 +0200, Friedrich Dominicus
> <···················@q-software-solutions.de> tried to confuse everyone with
> this message:
>
> >"Dmitriy Ivanov" <············@M_aha.ru> writes:
> >
> >> Hello folks,
> >>
> >> Is anybody aware of an open lisp code for converting "typical" forum
> >> messages to HTML?
> >>
> >> Message text can include pseudo-tags like [b]...[/b],
> >> [url=address]...[/url]
> >> and the like. Additionally, empty lines usually indicate a new paragraph
> >> and
> >> so on.
> >Hm isn't that wiki markup? So a wiki for Common Lisp should contain
> >that code
> >
>
> No, it's message boards markup. I'm currently implementing one in Common
> Lisp.
I think the common name for it is "BBcode".
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***