[plt-scheme] HTML: how to get structure name?

From: Veer (diggerrrrr at gmail.com)
Date: Sun Apr 26 03:16:42 EDT 2009

Thanks , struct->vector will do , I was trying struct-info to get the symbol.

I was thinking of making reusable html widget , for example
text edit box , list box , menu etc.

What i was looking to do(roughly) was, to create a general input%
class , then derive a specialized text-edit% class from it.
In the end everything goes into (make-html ...) , which i then convert to xexpr.
This where i needed the symbol. I am not sure if this correct way to
convert html structs to xexpr.

Roughly something like this :

#lang scheme
(require xml)
(require html)

(define input%
  (class object%
    (init ((itype type)) ((iname name)) ((ivalue value)))
    (define input-type (make-attribute #f #f 'type itype))
    (define input-name (make-attribute #f #f 'name iname))
    (define input-value (make-attribute #f #f 'value ivalue))

    (super-new)

    (define/public (get-element)
      (make-input (list input-type input-name input-value)))
    ;... other methods ....

    ))


(define text-edit%
  (class input%
    (init [(ilabel label)])
    (define a-label ilabel)

    (define class-attr
      (make-attribute #f #f 'class "text-edit"))

    (define (to-pcdata str)
      (make-pcdata #f #f str))

    (super-new (type "text") (name "text-control") (value ""))

    (define/override (get-element)
      (make-div (list class-attr)
                (list (to-pcdata a-label) (super get-element))))))




;elem-name : html-element -> symbol
(define (elem-name elem)
  (let ([v (struct->vector elem)])
    (let ([str (symbol->string (vector-ref v 0))])
      (string->symbol (second (regexp-split ":" str))))))


;parse-contents : (listof html-content) ->(listof xml-element)
(define (parse-contents loc)
  (cond
    [(empty? loc) empty]
    [(pcdata? (first loc))
     (cons (first loc) (parse-contents (rest loc)))]
    [(entity? (first loc))
     (cons (first loc) (parse-contents (rest loc)))]
    [(html-element? (first loc))
     (cons (html-elem->xml-elem (first loc))
           (parse-contents (rest loc)))]
    [else (error "oops")]))

;html-elem->xml-elem : html-element -> xml-element
(define (html-elem->xml-elem elem)
  (cond
    [(html-full? elem)
     (make-element #f #f
                   (elem-name elem)
                   (html-element-attributes elem)
                   (parse-contents (html-full-content elem)))]

    [else (make-element #f #f
                        (elem-name elem)
                        (html-element-attributes elem) empty)]))


;to-xexpr : html-element -> xexpr
(define (to-xexpr elem)
  (xml->xexpr (html-elem->xml-elem elem)))


(define edit-box (new text-edit% [label "First Name:"]))

(define a-body (make-body empty (list (send edit-box get-element))))
(define a-html (make-html empty (list a-body)))

(to-xexpr a-html)



Veer.




On Sun, Apr 26, 2009 at 3:00 AM, Chongkai Zhu <czhu at cs.utah.edu> wrote:
> First I would ask what you want to do. Getting a symbol to indicate the
> struct type doesn't looks to be the best solution to be.
>
> But
>
> (vector-ref (struct->vector a-input) 0)
>
> will give you pretty much what you want, and it is easy to remove the
> `struct:' prefix from a symbol if have to.
>
> Chongkai
>
> Veer wrote:
>>
>> Hello,
>>
>> Is it possible to get the name of html structures as symbol?
>> For example :
>>
>> (require html)
>> (define a-input (make-input empty))
>>
>> >From a-input i need to get symbol  'input .
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>
>


Posted on the users mailing list.