View documentation | View discussion [135 posts] | View script | License |
Download script | History | Other scripts by: inetw3dm |
30-Apr 16:08 UTC
[0.043] 15.171k
[0.043] 15.171k
Archive version of: quote-node-attributes.r ... version: 7 ... inetw3dm 14-Apr-2021REBOL [ File: %quote-node-attributes.r Date: 23-11-2020 Title: "Dialect Object Model" Author: daniel murrill Emai: inetw3.dm@gmail.com Purpose: { This script is used on variable values, key value pairs that are written in different syntax styles such as <tags> in html,xml,css>, objects, arrays, json, rebol series, etc. It will properly format mixed, malformed, key value data found in a DSL that's is usually in the format of an object, array, or json statement. It requotes *" "* and creates it as a taged node-element for look up in a Dialect Object Model to be used with rebol-DOM functions.} ] library: [ level: 'intermediate platform: 'all type: function domain: [html vid css json js array] tested-under: 'windows Author: daniel murrill support: none license: none see-also: %HTML-view.r ] ;;;;;this should use "quote-node-attributes: func [in-node-element] in order to select a ;;;;;paramater to quote-to-tag. tag-tokens: to-hash [ style-obj-chars [ "<" "" {style="} " " {style=} " " " : " { "} ": " { "} ":" {*"} " " {__} {=""} " " "=" {*"} ";" {"*} {""} {"} { "} {"} {*"} { "} {"*} {" } " >" "" ">" "" "rgb" "" {__"} {"} {" "} {"} ] ] remove-tail-char?: func [in-node-element element][=: :equal? if element = char: back tail trim/tail in-node-element [remove trim/tail char] ] quote-node-attributes: does [with-these-attributes: copy "" remove-tail-char? in-node-element </> this-node: find/match in-node-element node-name: first parse/all in-node-element { =">} replace/all this-node {:"} {"} replace/all this-node {,} " " if find/any this-node "style=" [ parse/all this-node [[to {style="} | to "style="] copy style-obj [thru {;" } | thru {;">} | thru {;>} | thru {;"} | thru {" } | thru {>} | to end](*style.node: copy style-obj foreach [style-chars found-with-in] tag-tokens/style-obj-chars [replace/all *style.node style-chars found-with-in] replace this-node style-obj *style.node )] ] replace node-name "<" "" ;replace/all this-node {=""} {=null} replace/all this-node "://" "&&" foreach part replace/all this-node: parse/all this-node {<{} "=:;[]>} [""] [] [ part: any [if find/match part "'" [ replace/all part "'" "" ] part ] ;replace part {,} " " replace/all part {__} " " replace/all part "&&" "://" replace/all part ". " "." replace part {=null} {=""} append with-these-attributes mold part ] ] create-tag-element: func [these-attr][ in-attribute-blk: to-block these-attr foreach [attr-name attr-value] in-attribute-blk [ replace in-attribute-blk attr-name to-word attr-name attempt [trim attr-value] ] attempt [insert in-attribute-blk to-word trim/all strip-chars-from node-name] data-node: build-tag in-attribute-blk ] with-these-attributes: copy "" ;In the DOM everything grouped is a node. individual key value pairs are elements of a group. ;so we are *in a *node to requote *elements ;How to use: "this is a node full of messed up elements from a bad robot." in-node-element: {p "color":= "#0000ff" bgcolor="yellow",, "width" : "399" "height":"100", "font-styles" : "big" style={"color: green; bgcolor: purple;"} } quote-node-attributes create-tag-element with-these-attributes ;or... ;change "quote-node-attributes: does" to "quote-node-attributes: func [in-node-element]" ;You also can try using the Dialect Object Model *Var on your key/value data without having ;to know or care if it's malformed. It will select proper key values for mulyiple syntax styles ;written as strings and blocks. ;Var contacts: = {Bestfriend: "[{"first": "Besty", "last": "Bestest", "number": 777-9211}]"} ;Bestfriend[first] or.. contacts: :array-obj! ;contacts[first] Notes
|