Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rebol [
- date: 24-10-2013
- title: "Markup"
- author: "Nicolas Schmidt"
- version: 1.1
- note: { Updated for rebol 3 }
- ]
- build-tag: func [
- "Generates a tag from a composed block."
- values [block!] "Block of parens to evaluate and other data."
- /local tag value-rule xml? name attribute value
- ][
- tag: make string! 7 * length? values
- value-rule: [
- set value issue! (value: mold value)
- | set value file! (value: replace/all copy value #" " "%20")
- | set value any-type!
- ]
- xml?: false
- parse compose values [
- [
- set name ['?xml (xml?: true) | word!] (append tag name)
- any [
- set attribute [word! | url!] value-rule (
- repend tag [#" " attribute {="} value {"}]
- )
- | value-rule (repend tag [#" " value])
- ]
- end (if xml? [append tag #"?"])
- ]
- |
- [set name refinement! to end (tag: mold name)]
- ]
- to tag! tag
- ]
- build-tags-no-format: func [
- {main recursive function}
- arg [block!] out [block!]
- /local start content end
- ][
- forall arg [
- start: arg/1
- start: append copy [] start
- end: append copy [] to-refinement first start
- repend out [ start: build-tag start ]
- if not-equal? #"/" last start [
- arg: next arg
- content: arg/1
- either block? content [
- append out build-tags content copy []
- ] [
- repend out [content]
- ]
- repend out [ build-tag end]
- ]
- ]
- out
- ]
- {;example
- build-tags-no-format [html [head [ [a href arstechnica.com ] {a link} ] ] ] []
- == [<html> <head> <a href="arstechnica.com"> "a link" </a> </head> </html>]
- }
- build-tags: func [
- {main recursive function for markup}
- arg [block!] out tabs
- /local start content end
- ][
- forall arg [
- start: arg/1
- start: append copy [] start
- end: append copy [] to-refinement first start
- repend out [ tabs start: build-tag start newline ]
- if not-equal? #"/" last start [
- arg: next arg
- content: arg/1
- either block? content [
- append out build-tags content copy {} join tabs tab
- ] [
- repend out [ join tabs tab content newline ]
- ]
- repend out [ tabs build-tag end newline ]
- ]
- ]
- out
- ]
- markup: func [
- {
- produce formatted markup from rebol blocks
- e.g.
- markup [tag [ tag2 "something" [hr /] ] ]
- => <tag><tag2>something</tag2><hr /></tag>
- Single tags, i.e. those without an end tag, must be enclosed a block and must have a #"/" character before the end brace.
- e.g. [meta /]
- If attributes are required then enclose the tag in braces. e.g. [a href google.com]
- }
- a
- ] [
- build-tags a copy {} copy {}
- ]
- { ; example
- print markup [
- html [
- head [
- style {}
- [meta-example attr value /]
- ]
- body [
- [a href arstechnica.com] {arstechnica}
- [br /] ; single tag
- [hr /]
- table [
- tr [td {something} td {else}]
- tr [td {now} td {isn't} td {good}]
- tr []
- ]
- [hr /]
- ]
- ]
- ] }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement