From: André Næss
Subject: Turning symbols into strings (lisp as markup)
Date:
Message-ID: <b7fats$k0o$1@maud.ifi.uio.no>
I'm a lisp newbie who has only been playing around with emacs lisp for a
short while. Now I want to try the real thing. Since one of the things I do
a lot is write HTML documents I was thinking I could write a small tool to
convert documents with a lisp like structure into HTML. I guess this has
been done hundreds of times before, but I'm thinking it can be a nice way
of getting some hands-on experience.
What I'm imagining is rather simple, say we have a micro markup language
with the tags p, b and em, which all map directly onto HTML. I could then
write:
(p This is my text with (b bold) and (em emphasize)
You know what? 2 + 2 is (+ 2 2))
And this would expand into
<p>This is my text with <b>bold</b> and <em>emphasize</p>
The problem is that I don't want to put "" around all my text as that would
make it much more tedious to use this microML. So I'm wondering: Is there a
way to achieve this using read-macros or normal macros, or will I have to
resort to a preprocessing stage to turn the simple example above into:
(p "This is my text with" (b "bold") "and" (em "emphasize")
"You know what? 2 + 2 is" (+ 2 2))
before evaluating it. The rule here is that whenever a '(' is seen (not
escaped) I'll handle the rest as an S-expression, if the first symbol in
the S-expression is one of the language macros (i.e. p, b or em) then
everything following it should be treated as strings, unless a '(' is seen,
and so on...
In other words, the macros basically accept lists of "stuff" which they turn
into strings and return enclosed in the appropriate HTML tag.
Attributes are for level 2 ;)
Your input would be much appreciated.
Andr� N�ss
In article <············@maud.ifi.uio.no>,
Andr� N�ss <·······················@ifi.uio.no> wrote:
>I'm a lisp newbie who has only been playing around with emacs lisp for a
>short while. Now I want to try the real thing. Since one of the things I do
>a lot is write HTML documents I was thinking I could write a small tool to
>convert documents with a lisp like structure into HTML. I guess this has
>been done hundreds of times before, but I'm thinking it can be a nice way
>of getting some hands-on experience.
Yes, it has been done before.
>What I'm imagining is rather simple, say we have a micro markup language
>with the tags p, b and em, which all map directly onto HTML. I could then
>write:
>
>(p This is my text with (b bold) and (em emphasize)
> You know what? 2 + 2 is (+ 2 2))
>
>And this would expand into
><p>This is my text with <b>bold</b> and <em>emphasize</p>
>
>The problem is that I don't want to put "" around all my text as that would
>make it much more tedious to use this microML. So I'm wondering: Is there a
>way to achieve this using read-macros or normal macros, or will I have to
>resort to a preprocessing stage to turn the simple example above into:
>
>(p "This is my text with" (b "bold") "and" (em "emphasize")
> "You know what? 2 + 2 is" (+ 2 2))
>
>before evaluating it. The rule here is that whenever a '(' is seen (not
>escaped) I'll handle the rest as an S-expression, if the first symbol in
>the S-expression is one of the language macros (i.e. p, b or em) then
>everything following it should be treated as strings, unless a '(' is seen,
>and so on...
>
>In other words, the macros basically accept lists of "stuff" which they turn
>into strings and return enclosed in the appropriate HTML tag.
SYMBOL-NAME or PRIN1-TO-STRING will return the name of a symbol as a
string. So your P macro can iterate through all the arguments -- if it's
an atom, call PRIN1-TO-STRING, otherwise EVAL it.
One problem, though: the Lisp reader normally uppercases symbols as it's
reading them. So you'll get the equivalent of
(P THIS IS MY TEXT WITH (B BOLD) AND (EM EMHASIZE) YOU KNOW WHAT? 2 + 2 IS
(+ 2 2))
--
Barry Margolin, ··············@level3.com
Genuity Managed Services, a Level(3) Company, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.