HTML Tag functions?

Started by jazper, December 13, 2013, 09:14:01 AM

Previous topic - Next topic

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<h1>string</h1>.  



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

xytroxon

#1
I did, here is some experimental code.



Put into file named sxmlhtml.lsp



out put = test.htm



--xytoxon[attachment=0]sxmlhtml.tar[/attachment]
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

hilti

#2
I like this approach, available on Github


Quote
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">

   <head>

      <title><% (print "This is my title") %></title>

   </head>

</html>



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

tag.



Further parameters are concatenated to second one.



Every function returns a string with the computed html code.


https://github.com/conan-lugmen/newlisp-hin">//https://github.com/conan-lugmen/newlisp-hin
--()o Dragonfly web framework for newLISP

http://dragonfly.apptruck.de\">http://dragonfly.apptruck.de

conan

#3
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:


QuoteEvery 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.

xytroxon

#4
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> ->
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

jazper

#5
Much appreciated, thanks xytotron.  I very much admire that code.  



The .lsp file ran straight off in Win 7, but returned
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.

jazper

#6
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.

jazper

#7
After testing, the code works when called from newLISP, so I'm not bothered about IDE problems.

xytroxon

#8
The only thing I can think of, is to try and change  the two places 26 is used to a different  number.



I remember that I had a strange problem on Windows when using 00 so I changed it tp 26



26 is the ascii code for end of file



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



(string "26" str ">")  -> (string "01" str ">")



(replace ">26" xml-string "")  -> (replace ">01" xml-string "")



Or try increasing the stack size when running newlisp.



-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

Astrobe

#9
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 (define (html) (tag "html" (args)). (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:
(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.

jazper

#10
I tried the changes suggested by xytotron (replacing 26 with 01), without success.



It still runs fine directly from newLISP or in Scite, which calls newLISP switched as follows: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.