Arc Forumnew | comments | leaders | submitlogin
1 point by thaddeus 5541 days ago | link | parent

So when I originally made this posting I was attempting to create a function that would create the style attributes within in an html div.

Since the style parameters/arguments are completely differrent for each of my divs, I found it a pain to keep doing this:

    (pr "<div " "id=")
	(write (string "divLeftBox"))  
	(pr "style=")
    (write (string "position: absolute; left: 1px; top:" 1 "px;" 
	(if (is border->toggle 1)(string "border: 1px solid #ff9000;"))
	 "width:" width "px; " "height: " height "px;"))
	(pr ">")
So I took a stab at writing a function, following, that would handle all the possible formats, but discovered the variances too ugly in coding without being able to handle the "inside optional arguments" for a function:

     (def genstyle-old (overflow position top right bottom left width width-uom height height-uom border-width border-color)
              (or= overflow 'auto)
	          (or= position 'absolute)
	          (or= top 0)
	          (or= right 0)
	          (or= bottom 0)
		      (or= left 0)
			  (or= width 100)
              (or= width-uom "%")
			  (or= height 100)
              (or= height-uom "%")
			  (or= border-width 1)
		      (= border-line-style 'solid)
			  (or= border-color "#ff9000")
              (string "overflow: " overflow ";" " position: " position ";"
                             " top: " top "px;" " right: " right "px;" " bottom: " bottom "px;" " left: " left "px;"
                             " width: " width width-uom ";" " height: " height height-uom ";" 
	                         " border: " border-width "px" " solid " border-color ";")
    )
Anyway that turned out too ugly and Kenny's dsb function was too complicated for me to figure out, so I went back and looked into how pg was doing these type of things... and came up with the following:

    (mac genfn args (genlist args))

    (def genlist (spec)
       `(tostring ,@(parg (pair spec)))
    )


    (def parg (options)
       (if (no options)
        nil
       (let ((opt val) . rest) options
            (cons (parg-it opt val)
                  (parg rest))))
    )

    (def parg-it (key val)
        `(aif ,val (pr " " ',key " " it ";"))
    )

this allows me to do this:

     (def test ()
         (= height 10) 
         (= width 20)
         (gentag div id "mydiv" style (genfn position: "absloute" overflow: "auto" width: (string width "px") height: (string height "px") padding: "2px 3px 2px 3px"))
     )
resulting in this:

    arc> (test)
    <div id="mydiv" style=" position: absloute; overflow: auto; width: 20px; height: 10px; padding: 2px 3px 2px 3px;">
* note the significance for me is that I can pull the style arguments from a table and then autogenerate the list to be passed into the genfn function based upon which attributes exist in the table; then all I would need to do is pass in the table name. The end result will be 1 function that generates all of my divs, table driven.

I figured there might be some people out there (like myself) trying to do the same thing. I don't know how to put things on "git", nor do I want to throw my "hacks" into a repository that's probably intended for better code. :)

T.