Advertisement
nicolas42

markup

Oct 23rd, 2013
2,937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
REBOL 3.15 KB | None | 0 0
  1. rebol [
  2.     date: 24-10-2013
  3.     title: "Markup"
  4.     author: "Nicolas Schmidt"
  5.     version: 1.1
  6.     note: { Updated for rebol 3 }
  7. ]
  8.  
  9. build-tag: func [
  10.     "Generates a tag from a composed block."
  11.     values [block!] "Block of parens to evaluate and other data."
  12.     /local tag value-rule xml? name attribute value
  13. ][
  14.     tag: make string! 7 * length? values
  15.     value-rule: [
  16.         set value issue! (value: mold value)
  17.         | set value file! (value: replace/all copy value #" " "%20")
  18.         | set value any-type!
  19.     ]
  20.     xml?: false
  21.     parse compose values [
  22.         [
  23.             set name ['?xml (xml?: true) | word!] (append tag name)
  24.             any [
  25.                 set attribute [word! | url!] value-rule (
  26.                     repend tag [#" " attribute {="} value {"}]
  27.                 )
  28.                 | value-rule (repend tag [#" " value])
  29.             ]
  30.             end (if xml? [append tag #"?"])
  31.         ]
  32.         |
  33.         [set name refinement! to end (tag: mold name)]
  34.     ]
  35.     to tag! tag
  36. ]
  37.  
  38.  
  39. build-tags-no-format: func [
  40.     {main recursive function}
  41.     arg [block!] out [block!]
  42.     /local start content end
  43. ][
  44.     forall arg [
  45.    
  46.         start: arg/1
  47.         start: append copy [] start
  48.         end: append copy [] to-refinement first start
  49.         repend out [ start: build-tag start ]
  50.        
  51.         if not-equal? #"/" last start [
  52.             arg: next arg
  53.             content: arg/1         
  54.             either block? content [
  55.                 append out build-tags content copy []
  56.             ] [
  57.                 repend out [content]
  58.             ]          
  59.             repend out [ build-tag end]
  60.         ]
  61.     ]
  62.     out
  63. ]
  64.  
  65. {;example
  66. build-tags-no-format [html [head [ [a href arstechnica.com ] {a link} ] ] ] []
  67. == [<html> <head> <a href="arstechnica.com"> "a link" </a> </head> </html>]
  68. }
  69.  
  70. build-tags: func [
  71.     {main recursive function for markup}
  72.     arg [block!] out tabs
  73.     /local start content end
  74. ][
  75.     forall arg [
  76.    
  77.         start: arg/1
  78.         start: append copy [] start
  79.         end: append copy [] to-refinement first start
  80.        
  81.         repend out [ tabs start: build-tag start newline ]
  82.  
  83.         if not-equal? #"/" last start [
  84.        
  85.             arg: next arg
  86.             content: arg/1
  87.            
  88.             either block? content [
  89.                 append out build-tags content copy {} join tabs tab
  90.             ] [
  91.                 repend out [ join tabs tab content newline ]
  92.             ]
  93.            
  94.             repend out [ tabs build-tag end newline ]
  95.         ]
  96.     ]
  97.     out
  98. ]
  99.  
  100. markup: func [
  101.     {
  102.     produce formatted markup from rebol blocks
  103.     e.g.
  104.     markup [tag [ tag2 "something" [hr /] ] ]
  105.      => <tag><tag2>something</tag2><hr /></tag>
  106.  
  107.     Single tags, i.e. those without an end tag, must be enclosed a block and must have a #"/" character before the end brace.
  108.     e.g. [meta /]
  109.    
  110.     If attributes are required then enclose the tag in braces. e.g. [a href google.com]
  111.     }
  112.     a
  113. ] [
  114.     build-tags a copy {} copy {}
  115. ]
  116.  
  117.  
  118. { ; example
  119.  
  120. print markup [
  121.     html [
  122.         head [
  123.             style {}
  124.             [meta-example attr value /]
  125.         ]
  126.         body [
  127.             [a href arstechnica.com] {arstechnica}
  128.             [br /] ; single tag
  129.             [hr /]
  130.             table [
  131.                 tr [td {something} td {else}]
  132.                 tr [td {now} td {isn't} td {good}]
  133.                 tr []
  134.             ]
  135.             [hr /]
  136.         ]
  137.     ]
  138. ] }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement