nydp-html 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40fa5ecff67b7c001d0e445507ce177ceadecf13
4
- data.tar.gz: c5850142ea885409dc663241084067325f4a4fe7
3
+ metadata.gz: 8d01a7d3f36a0d99679fd19d725ff85a643a6b06
4
+ data.tar.gz: dab0a28d59f58f676f0e646ad9a0aace4b86848d
5
5
  SHA512:
6
- metadata.gz: edd1ef38d177f7a64c73e344722c1a76d7909f768868be3b77b639f2c269f60c182ea3f00af84b53493c67c2908ea7d2b98e259201e99b90d690ef6aba6885fb
7
- data.tar.gz: 77add9b61ae9d7cba66cc4ed321870ad339610445a7d1e1342104108dcbf48bd276b4292e04e97c54c262e4f5f05cbe320f607aa8331f9375a67034b184e5846
6
+ metadata.gz: e03798eed60035ddeb288e1564a2d798b998dc7cc97c960c96dd67b6b273ac976813d4629b2c723d86ca699247eb4ac57c7ce03b734c1e78d80b3afb5ed37b94
7
+ data.tar.gz: 090d0f5628bc87d76854a6306c6d5b866175be73fce881d357fd543c36fe60b3b7d0b0e2b00582a74b668a99f57a727f7883fe782ea3db633ef4266dc83cd7b5
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Nydp::Html
2
2
 
3
- TODO: Write a gem description
3
+ Nydp::Html is the amazing HTML templating library for use with NYDP.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,7 +20,47 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ #### render-as-textile:
24
+
25
+ ```lisp
26
+ (render-as-textile "hello world") ;=> "<p>hello world</p>"
27
+ (render-as-textile (get-some-text-from 'somewhere)) ;=> (textile-to-html (get-some-text-from 'somewhere))
28
+ (render-as-textile "hello ~name") ;=> (string-pieces "<p>hello" name "<p>")
29
+ ```
30
+
31
+ #### render-as-haml:
32
+
33
+ ```lisp
34
+ (render-as-haml "%p hello world") ;=> "<p>hello world</p>"
35
+ (render-as-haml (get-some-text-from 'somewhere)) ;=> (haml-to-html (get-some-text-from 'somewhere))
36
+ (render-as-haml "%p hello ~name") ;=> (string-pieces "<p>hello" name "<p>")
37
+ ```
38
+
39
+ If you want to generate a function that returns html from a string that you've stored elsewhere (in a CMS for example),
40
+
41
+ ```lisp
42
+ ; assume 'content is the text as stored in your CMS
43
+
44
+ (mac make-renderer (name content)
45
+ `(def ,name ()
46
+ (textile-to-html ,(parse-in-string content))))
47
+
48
+ (make-renderer homepage "hello \~person - \"click here\":/buy-now to buy some stuff *now*")
49
+
50
+ (assign person "Cleopatra")
51
+
52
+ (homepage) ;=> returns "<p>hello Cleopatra &#8211; <a href=\"/buy-now\">click here</a> to buy some stuff <strong>now</strong></p>"
53
+ ```
54
+
55
+ Same idea if your CMS text uses HAML markup, just use 'haml-to-html instead of 'textile-to-html. NYDP uses tilde for string
56
+ interpolations, don't be inhibited:
57
+
58
+ ```lisp
59
+
60
+ (make-renderer homepage "hello ~(db-lookup current-user contact-info name), you have *~(shopping-cart.size)*
61
+ items in your shopping cart. ~(render-shopping-cart) %(call-to-action)~(buy-now-button)%")
62
+
63
+ ```
24
64
 
25
65
  ## Contributing
26
66
 
File without changes
@@ -0,0 +1,101 @@
1
+ (register-test
2
+ '(suite "haml"
3
+ ("build a function to render a given haml string with the given arguments"
4
+ (pre-compile '(render-as-haml ".funny#my-id hello ~context.name this file was in ~(just source.name)."))
5
+ (string-pieces
6
+ "<div class='funny' id='my-id'>hello "
7
+ (interpolate (hash-get context 'name))
8
+ " this file was in "
9
+ (interpolate (hash-get source 'name))
10
+ ".</div>\n"))
11
+
12
+ ("render a plain haml string to html"
13
+ (render-as-haml "%h1 header text
14
+
15
+ %p paragraph text
16
+
17
+ %ul
18
+ %li item 1
19
+ %li item 2
20
+ ")
21
+ "<h1>header text</h1>
22
+ <p>paragraph text</p>
23
+ <ul>
24
+ <li>item 1</li>
25
+ <li>item 2</li>
26
+ </ul>\n")
27
+
28
+ ("compile a string with interpolations to an instruction to generate haml"
29
+ (pre-compile '(render-as-haml "%h1 HEADER
30
+
31
+ %p ~a and ~b and ~(c 3)
32
+
33
+ %ul
34
+ %li item ~(assign counter (+ counter 1))
35
+ %li item ~(assign counter (+ counter 1))
36
+ %li item ~(assign counter (+ counter 1))
37
+ "))
38
+
39
+ (string-pieces "<h1>HEADER</h1>\n<p>"
40
+ (interpolate a) " and " (interpolate b) " and " (interpolate (c 3))
41
+ "</p>\n<ul>\n <li>item " (interpolate (assign counter (+ counter 1)))
42
+ "</li>\n <li>item " (interpolate (assign counter (+ counter 1)))
43
+ "</li>\n <li>item " (interpolate (assign counter (+ counter 1)))
44
+ "</li>\n</ul>\n" nil))
45
+
46
+ ("compile a function invocation to render as haml"
47
+ (pre-compile '(render-as-haml a))
48
+ (haml-to-html a))
49
+
50
+ ("compile a plain string with no interpolations to an instruction to generate haml"
51
+ (pre-compile '(render-as-haml "%h1 HEADER
52
+
53
+ %p para
54
+
55
+ %ul
56
+ %li item 1
57
+ %li item 2
58
+ "))
59
+ "<h1>HEADER</h1>
60
+ <p>para</p>
61
+ <ul>
62
+ <li>item 1</li>
63
+ <li>item 2</li>
64
+ </ul>
65
+ ")
66
+
67
+ ("compile a simple string with one interpolation to render html"
68
+ (pre-compile '(render-as-haml "hello ~name "))
69
+ (string-pieces "hello " (interpolate name) "\n"))
70
+
71
+ ("render a simple string with one interpolation to html"
72
+ (let name "conan"
73
+ (render-as-haml "hello ~name"))
74
+ "hello conan\n")
75
+
76
+ ("render a string with interpolations to html"
77
+ (with (a 1 b 2 c (fn (x) (* 2 x)) counter 0)
78
+ (render-as-haml "%h1 HEADER
79
+
80
+ %p ~a and ~b and ~(c 3)
81
+
82
+ %ul
83
+ %li item ~(assign counter (+ counter 1))
84
+ %li item ~(assign counter (+ counter 1))
85
+ %li item ~(assign counter (+ counter 1))
86
+ "))
87
+
88
+ "<h1>HEADER</h1>
89
+ <p>1 and 2 and 6</p>
90
+ <ul>
91
+ <li>item 1</li>
92
+ <li>item 2</li>
93
+ <li>item 3</li>
94
+ </ul>
95
+ ")
96
+
97
+ ("render a variable to html"
98
+ (with (a "%p hello, world") (render-as-haml a))
99
+
100
+ "<p>hello, world</p>\n")
101
+ ))
@@ -1,14 +1,13 @@
1
1
  (register-test
2
2
  '(suite "textile"
3
3
  ("build a function to render a given textile string with the given arguments"
4
- (pre-compile '(textile-build-function (a b c) "hello ~context.name this file was in ~(just source.name)."))
5
- (fn (a b c)
6
- (string-pieces
7
- "<p>hello "
8
- (interpolate (hash-get context 'name))
9
- " this file was in "
10
- (interpolate (hash-get source 'name))
11
- ".</p>")))
4
+ (pre-compile '(render-as-textile "hello ~context.name this file was in ~(just source.name)."))
5
+ (string-pieces
6
+ "<p>hello "
7
+ (interpolate (hash-get context 'name))
8
+ " this file was in "
9
+ (interpolate (hash-get source 'name))
10
+ ".</p>"))
12
11
 
13
12
  ("render a plain textile string to html"
14
13
  (render-as-textile "h1. header text
@@ -21,38 +20,38 @@ paragraph text
21
20
  "<h1>header text</h1>\n<p>paragraph text</p>\n<ul>\n\t<li>item 1</li>\n\t<li>item 2</li>\n</ul>")
22
21
 
23
22
  ("compile a string with interpolations to an instruction to generate textile"
24
- (compile-as-textile "h1. HEADER
23
+ (pre-compile '(render-as-textile "h1. HEADER
25
24
 
26
25
  ~a and ~b and ~(c 3)
27
26
 
28
27
  * item ~(assign counter (+ counter 1))
29
28
  * item ~(assign counter (+ counter 1))
30
29
  * item ~(assign counter (+ counter 1))
31
- ")
30
+ "))
32
31
 
33
- (string-pieces "<h1>HEADER</h1>\n<p>"
34
- (interpolate a) " and " (interpolate b) " and " (interpolate (c 3))
35
- "</p>\n<ul>\n\t<li>item " (interpolate (assign counter (+ counter 1)))
36
- "</li>\n\t<li>item " (interpolate (assign counter (+ counter 1)))
37
- "</li>\n\t<li>item " (interpolate (assign counter (+ counter 1)))
38
- "</li>\n</ul>" nil))
32
+ (string-pieces "<h1>HEADER</h1>\n<p>"
33
+ (interpolate a) " and " (interpolate b) " and " (interpolate (c 3))
34
+ "</p>\n<ul>\n\t<li>item " (interpolate (assign counter (+ counter 1)))
35
+ "</li>\n\t<li>item " (interpolate (assign counter (+ counter 1)))
36
+ "</li>\n\t<li>item " (interpolate (assign counter (+ counter 1)))
37
+ "</li>\n</ul>" nil))
39
38
 
40
39
  ("compile a function invocation to render as textile"
41
- (compile-as-textile a)
40
+ (pre-compile '(render-as-textile a))
42
41
  (textile-to-html a))
43
42
 
44
43
  ("compile a plain string with no interpolations to an instruction to generate textile"
45
- (compile-as-textile "h1. HEADER
44
+ (pre-compile '(render-as-textile "h1. HEADER
46
45
 
47
46
  para
48
47
 
49
48
  * item 1
50
49
  * item 2
51
- ")
52
- "<h1>HEADER</h1>\n<p>para</p>\n<ul>\n\t<li>item 1</li>\n\t<li>item 2</li>\n</ul>")
50
+ "))
51
+ "<h1>HEADER</h1>\n<p>para</p>\n<ul>\n\t<li>item 1</li>\n\t<li>item 2</li>\n</ul>")
53
52
 
54
53
  ("compile a simple string with one interpolation to render html"
55
- (compile-as-textile "hello ~name ")
54
+ (pre-compile '(render-as-textile "hello ~name "))
56
55
  (string-pieces "<p>hello " (interpolate name) "</p>"))
57
56
 
58
57
  ("render a simple string with one interpolation to html"
@@ -77,5 +76,4 @@ para
77
76
  (with (a "hello, world") (render-as-textile a))
78
77
 
79
78
  "<p>hello, world</p>")
80
-
81
79
  ))
@@ -0,0 +1,47 @@
1
+ ;
2
+ ; render-as-textile:
3
+ ;
4
+ ; (render-as-textile "hello world") ;=> "<p>hello world</p>"
5
+ ; (render-as-textile (get-some-text-from 'somewhere)) ;=> (textile-to-html (get-some-text-from 'somewhere))
6
+ ; (render-as-textile "hello ~name") ;=> (string-pieces "<p>hello" name "<p>")
7
+ ;
8
+ ; render-as-haml:
9
+ ;
10
+ ; (render-as-haml "%p hello world") ;=> "<p>hello world</p>"
11
+ ; (render-as-haml (get-some-text-from 'somewhere)) ;=> (haml-to-html (get-some-text-from 'somewhere))
12
+ ; (render-as-haml "%p hello ~name") ;=> (string-pieces "<p>hello" name "<p>")
13
+ ;
14
+
15
+ (def interpolate (arg) arg)
16
+
17
+ (def html-process-parts (parts converter)
18
+ (let separator (random-string 20)
19
+ (string-split (converter.method (joinstr "~(just converter.esc)~(just separator)~(just converter.esc)"
20
+ parts))
21
+ separator)))
22
+
23
+ (def html-interpolatify-arg (tuple)
24
+ (if (cadr tuple)
25
+ `(interpolate ,(cadr tuple))
26
+ nil))
27
+
28
+ (def html-build-interpolator (pieces converter)
29
+ (let tuples (pairs pieces)
30
+ `(string-pieces ,@(apply + (zip (html-process-parts (map car tuples) converter)
31
+ (map html-interpolatify-arg tuples))))))
32
+
33
+ (mac render-as-haml (arg)
34
+ (if (isa 'string arg)
35
+ (haml-to-html arg)
36
+ (and (pair? arg)
37
+ (eq? 'string-pieces (car arg)))
38
+ (html-build-interpolator (cdr arg) { method haml-to-html esc "" })
39
+ `(haml-to-html ,arg)))
40
+
41
+ (mac render-as-textile (arg)
42
+ (if (isa 'string arg)
43
+ (textile-to-html arg)
44
+ (and (pair? arg)
45
+ (eq? 'string-pieces (car arg)))
46
+ (html-build-interpolator (cdr arg) { method textile-to-html esc "==" })
47
+ `(textile-to-html ,arg)))
@@ -1,3 +1,4 @@
1
+ require "haml"
1
2
  require "redcloth"
2
3
  require "nydp"
3
4
  require "nydp/literal"
@@ -16,7 +17,7 @@ module Nydp
16
17
  end
17
18
 
18
19
  def loadfiles
19
- b = relative_path('../lisp/textile.nydp')
20
+ b = relative_path('../lisp/to-html.nydp')
20
21
  [b]
21
22
  end
22
23
 
@@ -26,6 +27,27 @@ module Nydp
26
27
 
27
28
  def setup ns
28
29
  Symbol.mk("textile-to-html", ns).assign(Nydp::Html::TextileToHtml.new)
30
+ Symbol.mk("haml-to-html", ns).assign(Nydp::Html::HamlToHtml.new)
31
+ end
32
+ end
33
+
34
+ class HamlToHtml
35
+ def convert_from_haml convertible
36
+ Haml::Engine.new(convertible, suppress_eval: true).render
37
+ rescue Exception => e
38
+ if e.line
39
+ lines = convertible.split(/\n/)
40
+ beginning = e.line - 2
41
+ beginning = 0 if beginning < 0
42
+ selection = lines[beginning...(e.line + 1)].join "\n"
43
+ "#{e.message}<br/>line #{e.line}<br/><br/><pre>#{selection}</pre>"
44
+ else
45
+ e.message
46
+ end
47
+ end
48
+
49
+ def invoke vm, args
50
+ vm.push_arg Nydp::StringAtom.new convert_from_haml(args.car.to_s)
29
51
  end
30
52
  end
31
53
 
@@ -1,5 +1,5 @@
1
1
  module Nydp
2
2
  module Html
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nydp-html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-07 00:00:00.000000000 Z
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,8 +122,10 @@ files:
122
122
  - README.md
123
123
  - Rakefile
124
124
  - bin/nydp-tests
125
+ - lib/lisp/haml.nydp
126
+ - lib/lisp/tests/haml-tests.nydp
125
127
  - lib/lisp/tests/textile-tests.nydp
126
- - lib/lisp/textile.nydp
128
+ - lib/lisp/to-html.nydp
127
129
  - lib/nydp/html.rb
128
130
  - lib/nydp/html/version.rb
129
131
  - nydp-html.gemspec
@@ -1,71 +0,0 @@
1
- ;
2
- ; render-as-textile:
3
- ;
4
- ; ================================================================================
5
- ;
6
- ; the simplest case:
7
- ;
8
- ; (render-as-textile "hello world")
9
- ;
10
- ; expands to
11
- ;
12
- ; "<p>hello world</p>"
13
- ;
14
- ; ================================================================================
15
- ;
16
- ; next case,
17
- ;
18
- ; (render-as-textile (get-some-text-from 'somewhere))
19
- ;
20
- ; expands to
21
- ;
22
- ; (textile-to-html (get-some-text-from 'somewhere))
23
- ;
24
- ; ================================================================================
25
- ;
26
- ; the most useful case:
27
- ;
28
- ; (render-as-textile "hello ~name")
29
- ;
30
- ; expands to :
31
- ;
32
- ; (string-pieces "<p>hello" name "<p>")
33
- ;
34
- ;
35
-
36
- (def interpolate (arg) arg)
37
-
38
- (def textile-process-parts (parts)
39
- (let separator (random-string 20)
40
- (string-split (textile-to-html (joinstr "==~{separator}=="
41
- parts))
42
- separator)))
43
-
44
- (def textile-interpolatify-arg (tuple)
45
- (if (cadr tuple)
46
- `(interpolate ,(cadr tuple))
47
- nil))
48
-
49
- (def build-textile-interpolator (pieces)
50
- (let tuples (pairs pieces)
51
- `(string-pieces ,@(apply + (zip (textile-process-parts (map car tuples))
52
- (map textile-interpolatify-arg tuples))))))
53
-
54
- (mac compile-as-textile (arg)
55
- (if (isa 'string arg)
56
- (textile-to-html arg)
57
- (and (pair? arg)
58
- (eq? 'string-pieces (car arg)))
59
- `(build-textile-interpolator ,(quotify (cdr arg)))
60
- `(list 'textile-to-html ',arg)))
61
-
62
- (mac textile-build-function (param-names textile-string)
63
- `(fn ,param-names (render-as-textile ,textile-string)))
64
-
65
- (mac render-as-textile (arg)
66
- (if (isa 'string arg)
67
- (textile-to-html arg)
68
- (and (pair? arg)
69
- (eq? 'string-pieces (car arg)))
70
- (build-textile-interpolator (cdr arg))
71
- `(textile-to-html ,arg)))