literate-programming 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0579c7998d6a577731d88aaf496b921958e1c6c7
4
- data.tar.gz: 9e080bfd77245430a9cf245526651510d308c0c9
3
+ metadata.gz: 0e9b0f7406805966440cc4ae3043db64a8d08add
4
+ data.tar.gz: 67c2cc7eee3a515cb6d75a371c5e3dc79a17f4e1
5
5
  SHA512:
6
- metadata.gz: ef5992afcf34d3bfcf24608850b4d0d5f6cb9eab9989315fb22ebf3c739bbae6ed2f84c0ea8ef60b38f8c87d565b32b62e05743e9ea07f4b198a293991b31d37
7
- data.tar.gz: a54eb1c3414d1d6a74dffde4897dd9d898a6362d564acfcaecb8224502f2ee7a33f89b6a1090a30a2fa4b12befed28e5aa118a6a48cfd30aaa7873637a06090f
6
+ metadata.gz: 9742cc495c710c0737f81104f6c0c299b17670f1a7f4529d0ad2e390afc119237248f5cf719cfd6f60c6ad65ab42701902f5a80ce669b22d72e358f5899b263a
7
+ data.tar.gz: 976a254e9595a2ad8e89a463cd7a2efbf9a7aa4cf8d795fee37bab841588e6b37c49b7da9056b78fdbb1ee77f427409a9be30c3618816fef790c9e86627ca35d
data/README.md CHANGED
@@ -37,8 +37,7 @@ $ rweave example.wrb # to get example.md
37
37
  ```
38
38
 
39
39
  ## Examples
40
- - Hello, world!
41
-
40
+ ### Hello, world!
42
41
  First, write hellow.wrb
43
42
  ```
44
43
  First, define the Hello, world!
@@ -70,6 +69,77 @@ end
70
69
  main
71
70
  ```
72
71
 
72
+ ### 99 Bottles
73
+ This gem provides the Template Meta Programming.
74
+ For example, below code is an implementation of the 99-bottles.
75
+
76
+ ```
77
+ First, define the main function and it sings a song ''99 bottles of beer''.
78
+ [[*]] =
79
+ def main
80
+ [[bottles:100]]
81
+ end
82
+
83
+ General case, sing below
84
+ If the label-name end with ':@', the rtangle accepts the codes as a general template.
85
+ In the general template code,
86
+ - if '@'-mark followed by a number, the rtangle replaces it with an argument.
87
+ - if '@@'-mark followed by parents, the rtangle replaces it with return value of the insides.
88
+ [[bottles:@]] =
89
+ puts '@0 bottles of beer on the wall, @0 bottles of beer.'
90
+ puts 'Take one down and pass it around, @@(@0 - 1) bottles of beer on the wall.'
91
+ [[bottles:@@(@0 - 1)]]
92
+
93
+ The template code is also able to specialize.
94
+ If the rtangle does not find the specialized one,
95
+ it specializes from general template code like above.
96
+ When number of bottles == 2
97
+ [[bottles:2]] =
98
+ puts '2 bottles of beer on the wall, 2 bottles of beer.'
99
+ puts 'Take one down and pass it around, 1 bottle of beer on the wall.'
100
+ [[bottles:1]]
101
+
102
+ When number of bottles == 1
103
+ [[bottles:1]] =
104
+ puts '1 bottle of beer on the wall, 1 bottle of beer.'
105
+ puts 'Take one down and pass it around, no more bottles of beer on the wall.'
106
+ [[bottles:0]]
107
+
108
+ When no rest bottles...
109
+ [[bottles:0]] =
110
+ puts 'No more bottles of beer on the wall, no more bottles of beer.'
111
+ puts 'Go to the store and buy some more, 99 bottles of beer on the wall.'
112
+
113
+ Finally, call the main function.
114
+ [[*]] +=
115
+ main
116
+ ```
117
+
118
+ ### More Meta Method
119
+ This gem also provides lisp-like macro. Use *before* label to define functions
120
+ that returns eval-able string.
121
+
122
+ ```
123
+ If you want to do something that requires many sentence,
124
+ you can use *before* label;
125
+ It will be expanded and be evaluated by rtangle to help to write.
126
+ [[*]] =
127
+ def main
128
+ @@(helper_function)
129
+ end
130
+
131
+ For example, *before* label likes below;
132
+ Note: the *before*before* label, the *before*before*before* label, and so on, are also exists.
133
+ [[*before*]] =
134
+ def helper_function
135
+ return "p 'Hello, world!'"
136
+ end
137
+
138
+ Finally, call the main function.
139
+ [[*]] +=
140
+ main
141
+ ```
142
+
73
143
  ## License
74
144
 
75
145
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,18 @@
1
+ If you want to do something that requires many sentence,
2
+ you can use *before* label;
3
+ It will be expanded and be evaluated by rtangle to help to write.
4
+ [[*]] =
5
+ def main
6
+ @@(helper_function)
7
+ end
8
+
9
+ For example, *before* label likes below;
10
+ Note: the *before*before* label, the *before*before*before* label, and so on, are also exists.
11
+ [[*before*]] =
12
+ def helper_function
13
+ return "p 'Hello, world!'"
14
+ end
15
+
16
+ Finally, call the main function.
17
+ [[*]] +=
18
+ main
@@ -1,5 +1,5 @@
1
1
  module Literate
2
2
  class Programming
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -5,6 +5,12 @@ module Literate
5
5
  def initialize(src = "", source: nil, tabstop: 2)
6
6
  @source = source || src
7
7
  @tabstop = tabstop
8
+ @eval_context = BasicObject.new
9
+ @eval_context.instance_eval <<-EOC
10
+ def gensym
11
+ return (0 .. 20).collect { ('a' .. 'z')[rand(26)] }.join
12
+ end
13
+ EOC
8
14
  end
9
15
 
10
16
  def to_ruby
@@ -81,12 +87,13 @@ module Literate
81
87
  md << '```' << $/
82
88
  end
83
89
  table['*'] ||= ''
84
- return {rb: indent_code(expand(table)), md: indent_text(md)}
90
+ return {rb: indent_code(expand(table, '*')), md: indent_text(md)}
85
91
  end
86
92
 
87
- def expand(table)
93
+ def expand(table, label)
94
+ @eval_context.instance_eval(expand(table, label + 'before*')) if table.member? label + 'before*'
88
95
  processed = ''
89
- processing = '[[*]]'
96
+ processing = "[[#{label}]]"
90
97
  while processing.match /^((?:[^\[]|\[[^\[])*)\[\[([^\]]+)\]\]/m
91
98
  processed << $1
92
99
  expanding = $2
@@ -121,7 +128,7 @@ module Literate
121
128
  if follow.match /^(?<paren>\((?:[^()]|\g<paren>)*\))/m then
122
129
  # @@(expr) -> eval(expr)
123
130
  post_match = $~.post_match
124
- expanding = eval(expand_template $1, args)
131
+ expanding = @eval_context.instance_eval(expand_template $1, args)
125
132
  processed << expanding.to_s
126
133
  processing = post_match
127
134
  else
@@ -492,6 +492,61 @@ end
492
492
  Finally, instanciate Main and run it!
493
493
  ```ruby:* append
494
494
  Main.new.run
495
+ ```
496
+ EOC
497
+ end
498
+
499
+ it '*before* label' do
500
+ inst = Literate::Programming.new <<-EOM
501
+ If you want to do something that requires many sentence,
502
+ you can use *before* label;
503
+ It will be expanded and be evaluated by rtangle to help to write.
504
+ [[*]] =
505
+ def main
506
+ @@(helper_function)
507
+ end
508
+
509
+ For example, *before* label likes below;
510
+ Note: the *before*before* label, the *before*before*before* label, and so on, are also exists.
511
+ [[*before*]] =
512
+ def helper_function
513
+ return "p 'Hello, world!'"
514
+ end
515
+
516
+ Finally, call the main function.
517
+ [[*]] +=
518
+ main
519
+ EOM
520
+
521
+ expect(inst.to_ruby).to eq <<-EOC
522
+ def main
523
+ p 'Hello, world!'
524
+ end
525
+
526
+ main
527
+ EOC
528
+
529
+ expect(inst.to_md).to eq <<-EOC
530
+ If you want to do something that requires many sentence,
531
+ you can use *before* label;
532
+ It will be expanded and be evaluated by rtangle to help to write.
533
+ ```ruby:*
534
+ def main
535
+ @@(helper_function)
536
+ end
537
+ ```
538
+
539
+ For example, *before* label likes below;
540
+ Note: the *before*before* label, the *before*before*before* label, and so on, are also exists.
541
+ ```ruby:*before*
542
+ def helper_function
543
+ return "p 'Hello, world!'"
544
+ end
545
+ ```
546
+
547
+ Finally, call the main function.
548
+ ```ruby:* append
549
+ main
495
550
  ```
496
551
  EOC
497
552
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: literate-programming
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pixie-grasper
@@ -67,6 +67,7 @@ files:
67
67
  - Rakefile
68
68
  - bin/rtangle
69
69
  - bin/rweave
70
+ - example/labels-before.wrb
70
71
  - example/simple-1.wrb
71
72
  - example/simple-2.wrb
72
73
  - example/simple-3.wrb