Page 1 of 1

HTML Tag functions?

Posted: Fri Dec 13, 2013 5:14 pm
by jazper
Some time ago, one of the frequent contributors posted some code showing how to do HTML tags. I think it was in a context for each tag. Since bookmarking that, my hard disk died, and some bookmarks with it, though I had copies of all the main stuff.

Please can someone point me to this post, or the code in it? For example, a function called as (h1 string) would produce

Code: Select all


It was better than this, though, because it included args, allowing for classes etc.

Re: HTML Tag functions?

Posted: Fri Dec 13, 2013 9:54 pm
by xytroxon
I did, here is some experimental code.

Put into file named sxmlhtml.lsp

out put = test.htm

(6 KiB) Downloaded 230 times

Re: HTML Tag functions?

Posted: Sun Dec 15, 2013 11:17 am
by hilti
I like this approach, available on Github
HTML Domain Specific Language (DSL) for newLISP

The idea is to be able to write newlisp code as if you were writing HTML,
avoiding the use of template tags.

So instead of writing:

<html lang="en">
<title><% (print "This is my title") %></title>

Were you have to use an opening <% and a closing %> each time you need to
insert newlisp code, you write:

(html "lang=en"
(head ""
(title "" "This is my title")))

And everything is newlisp code.

To achieve this every HTML5 tag has it's counterpart function.

Every function needs at least one parameter for the inner code, hence the empty
strings in the example above. Also they can have any number of parameters
after, which are considered to be the code or text between opening and closing

Further parameters are concatenated to second one.

Every function returns a string with the computed html code.

Re: HTML Tag functions?

Posted: Sun Dec 15, 2013 3:36 pm
by conan
I haven't touched that code in some time now. Furthermore I have some un-pushed changes here in my disc, I changed several things to make it easier to use, for example, to avoid:
Every function needs at least one parameter for the inner code, hence the empty
strings in the example above.
There's no need for that in the new code.

However I stumped into some hard-to-debug (for me, cause I'm a moron) stuff which I cannot recall right now and then life got me away from this project.

What xytoxon posted works nice. My approach convoluted things just to have this one benefit: to validate HTML code at generation. However with all this TDD approach everybody has jumped in, which include tests for views, I'm not sure anymore if my approach adds any value.

So I'm kind of reluctant to review that code. But if someone here has interest. I can at least push the changes I got and a note with the failing bits, so someone can start from there.

Re: HTML Tag functions?

Posted: Sun Dec 15, 2013 8:49 pm
by xytroxon
Any improvements are welcome! I am unable to spend much time on it (or the internet) right now. I like how the recursiveness collapses into a single string, and being able to use newLISP comments too. Of course, the method of sticking the attributes into <tag></tag> isn't too elegant, but it works after a fashion ;o)

-- xytroxon

P.S. You can run the string through replace for <br></br> -> <br />

Re: HTML Tag functions?

Posted: Tue Dec 17, 2013 8:34 pm
by jazper
Much appreciated, thanks xytotron. I very much admire that code.

The .lsp file ran straight off in Win 7, but returned

Code: Select all

ERR: list or string expected : nil
called from user defined function page1:pre
called from user defined function page1:b
called from user defined function page1:p
called from user defined function page1:body
called from user defined function page1:html
called from user defined function page1:xml-document
on my 64 bit Crunchbang box. I guess that's line endings when unzipping or something. Will have a look tomorrow when I'm fresh.

Re: HTML Tag functions?

Posted: Wed Dec 18, 2013 4:57 am
by jazper
Interesting. Just gets curiouser and curiouser. On my Win 7 Dell at home, ran sxmlhtml.lsp in Scite: it ran perfectly, and produced test.html without a hitch. But newLISP GS editor produced exactly the error found on my Crunchbang 64 bit box.

Re: HTML Tag functions?

Posted: Thu Dec 19, 2013 11:20 pm
by jazper
After testing, the code works when called from newLISP, so I'm not bothered about IDE problems.

Re: HTML Tag functions?

Posted: Thu Dec 19, 2013 11:35 pm
by xytroxon
The only thing I can think of, is to try and change the two places \026 is used to a different number.

I remember that I had a strange problem on Windows when using \000 so I changed it tp \026

\026 is the ascii code for end of file

Try \001 or any number less than the value for space (Avoid the decimal values for "\t" "\r" "\n").

(string "\026" str ">") -> (string "\001" str ">")

(replace ">\026" xml-string "") -> (replace ">\001" xml-string "")

Or try increasing the stack size when running newlisp.

-- xytroxon

Re: HTML Tag functions?

Posted: Fri Dec 20, 2013 3:30 pm
by Astrobe
I started to do something along these lines. The technique is similar to the one used by xytroxon, except the tags generate a tree of strings. Then a "render" procedure walks the tree and spit it out. I don't have the code because it's on my PC atm.

For tag attributes, after various attempts, I came up with a macro named "with:", which sets a global variable to be inserted by the "tag" procedure that implements the tags. Most tags are basically like

Code: Select all

(define (html) (tag "html" (args))

Code: Select all

(set 'html (curry "html"))
would have been cool here, but it doesn't work there because the rest of the arguments are the body, and curry only works on 2-arguments procedures.

So the code for a page writes:

Code: Select all

(html (body
(with: "type=submit" (form "etc."))))
The drawback is that the order is reversed compared to regular html.The advantage is that one deosn't have 'noise' parameters. "with:" has to be a macro because newlisp evaluates the arguments first, and the global is not set a the right time.

Re: HTML Tag functions?

Posted: Mon Dec 23, 2013 9:17 pm
by jazper
I tried the changes suggested by xytotron (replacing \026 with \001), without success.

It still runs fine directly from newLISP or in Scite, which calls newLISP switched as follows:

Code: Select all

newlisp -n -c -s 400000
Since it runs fine from newLISP without setting a stack size, I haven't tried to find or fiddle with defaults called from the IDE. For my next trick, a hex editor will be used to see if there are line end problems. I remember fixing something like this in the past.