REBOL [ Author: "Brett Handley" Date: 23-May-2002 Version: 1.2.0 Email: brett@codeconscious.com Purpose: "Interprets rebol tags and returns them in another form." Comment: { Attempts to handle empty element type tags, but has a problem with these tags if they contain unquoted attribute values (which I believe are illegal anyway). Also note that if a tag extends across lines, this routine will strip the newline from the tag data. This would cause a side effect in any routine attempting to recreate the original stream from the data returned by the import-tag function. In most cases, I doubt that you'll find this an issue. Use it like so... import-tag Grab an attribute value select import-tag 'id or maybe... html: load/markup http://www.rebol.com foreach e html [ either tag? e [ if select import-tag e 'src [print select import-tag e 'alt] ][ false ] ] This one might be handy for tidying up those nasty unquoted values... build-tag import-tag } History: [ 1.2.1 [6-Nov-2002 {Abandoned the issue! change - sorry.} "Brett Handley"] 1.2.0 [23-may-2002 {Changed to issue! in output (to align with form-xml.r)} "Brett Handley"] 1.1.0 [06-may-2002 {Changed attribute words to lit-words in output.} "Brett Handley"] 1.0.6 [02-may-2002 {Removed two redundant word definitions.} "Brett Handley"] 1.0.5 [13-Mar-2001 {Fixed bug with attributes that have no values} "Brett Handley"] 1.0.4 [13-Mar-2001 {Fixed bug with newlines returned inside tag} "Brett Handley"] 1.0.3 [10-Jan-2001 { Capture ?xml... tags now. } "Brett Handley"] 1.0.2 [07-Nov-2000 { OOPS!. NOW copes with "!" tags - hopefully. } "Brett Handley"] 1.0.1 [05-Oct-2000 { Now copes with "!" tags and empty-element tags. } "Brett Handley"] 1.0.0 [30-Jun-2000 { Original code } "Brett Handley"] ] Rights: {Copyright © Brett Handley 2001 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, subrights, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.} ] unset [tag-language] ; ; Set up an ability to interpret tags. ; tag-importer: make object! [ tag-block-formatter: make object! [ result: none initialise: func [] [ result: copy [] ] new: func [name] [ append/only result :name ] set-as-empty: func [] [ append result first [/] ] attr: func [attr-name attr-value] [ append result to-lit-word :attr-name if attr-value [ append/only result :attr-value ] ] close: func [name] [ append/only result to-word join "/" :name ] uninterpreted: func [token] [ append result token ] ] tag-object-formatter: make object! [ result: none initialise: func [] [ result: make object! [ type: none name: none attr: copy [] ] ] new: func [name] [ result/type: 'open result/name: :name ] set-as-empty: func [] [result/type: 'empty] attr: func [attr-name attr-value] [ append/only result/attr :attr-name append/only result/attr :attr-data ] close: func [name] [ result/type: 'close result/name: :name ] uninterpreted: func [token] [ result/type: 'uninterpreted result/name: token ] ] emitter: tag-block-formatter token: none tag-type: none attr-name: none attr-data: none expr-data: none expr-data-start: none expr-data-end: none expr-asrebol: none result: none qt1: "'" qt2: {"} space: charset [#"^-" #"^/" #"^M" #" "] unquoted-data: complement space sus-chars: charset [#"À" - #"Ö" #"Ø" - #"ö" #"ø"] ; Not sure if to include these in letter digit: charset [#"0" - #"9"] letter: union charset [#"A" - #"Z" #"a" - #"z"] sus-chars data-chars: xml-language/data-chars data-chars-qt1: exclude data-chars charset [#"'"] data-chars-qt2: exclude data-chars charset [#"^""] name-first: union letter charset [#":" #"_"] name-chars: union union name-first digit complement charset [#"^-" #"^/" #" " #"="] sp: [some space] sp?: [any space] eq: [sp? #"=" sp?] name: [name-first any name-chars] xmlheader-tag: [ "?xml" copy token to end (emitter/uninterpreted head insert copy head token "?xml") ] bang-tag: [ #"!" copy token to end (emitter/uninterpreted head insert copy head token #"!") ] close-tag: [ #"/" (token: none) name-rule (emitter/close :token) ] new-tag: [ (token: none) name-rule (emitter/new :token) any [sp attribute] sp? opt ["/" (emitter/set-as-empty)] ] attribute: [ (token: none attr-name: none) name-rule (attr-data: none) opt [eq attr-value] (emitter/attr :token :attr-data) ] name-rule: [copy token name (token: to-word token)] attr-value: [ [qt1 copy attr-data any data-chars-qt1 qt1] | [qt2 copy attr-data any data-chars-qt2 qt2] | [copy attr-data some unquoted-data ] ] tag-rule: [ xmlheader-tag | bang-tag | close-tag | new-tag ] set 'import-tag func [ a-tag [tag!] /object "Returns result as an object." ][ either object [ emitter: tag-object-formatter ][ emitter: tag-block-formatter ] emitter/initialise if parse/all/case a-tag tag-rule [ emitter/result ] ] ] tag-object-to-block: function [ tag [object!] /set-words ][result-block][ result-block: copy [] either equal? tag/type 'close [ append result-block to-word join "/" tag/name ][ append result-block tag/name ] foreach [a-n a-v] tag/attr [ append result-block either set-words [to-set-word a-n][a-n] either a-v [ append result-block a-v ][ if set-words [ append result-block true ] ] ] if equal? tag/type 'empty [ append result-block first [/] ] result-block ]