textutils 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.markdown +1 -1
- data/Manifest.txt +3 -0
- data/README.markdown +7 -7
- data/lib/textutils/filter/code_filter.rb +46 -0
- data/lib/textutils/filter/erb_django_filter.rb +124 -0
- data/lib/textutils/filter/erb_filter.rb +14 -0
- data/lib/textutils.rb +5 -1
- metadata +6 -3
data/History.markdown
CHANGED
data/Manifest.txt
CHANGED
data/README.markdown
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
## Filters
|
4
4
|
|
5
|
-
### `comments_percent_style`
|
5
|
+
### `comments_percent_style` Filter
|
6
6
|
|
7
|
-
Strip
|
7
|
+
Strip comment lines starting with percent (that is, %). Example:
|
8
8
|
|
9
9
|
%%%%%%%%%%%%%%%%
|
10
10
|
% Some Headers
|
@@ -42,13 +42,13 @@ or
|
|
42
42
|
in plain old CSS. Thanks to gradient support in backgrounds in CSS3.
|
43
43
|
%end
|
44
44
|
|
45
|
-
Note: As a shortcut using a single `%end` directive (that is, without a
|
45
|
+
Note: As a shortcut using a single `%end` directive (that is, without a leading `%begin`)
|
46
46
|
will skip everything until the end of the document.
|
47
47
|
|
48
48
|
|
49
|
-
### `skip_end_directive`
|
49
|
+
### `skip_end_directive` Filter
|
50
50
|
|
51
|
-
Skip
|
51
|
+
Skip (comment out) text blocks in your document by
|
52
52
|
enclosing with `__SKIP__`/`__END__`. Example:
|
53
53
|
|
54
54
|
__SKIP__
|
@@ -57,7 +57,7 @@ enclosing with `__SKIP__`/`__END__`. Example:
|
|
57
57
|
in plain old CSS. Thanks to gradient support in backgrounds in CSS3.
|
58
58
|
__END__
|
59
59
|
|
60
|
-
As a shortcut
|
60
|
+
Note: As a shortcut using just `__END__` (without `__SKIP__`)
|
61
61
|
will skip everything from `__END__` until the end of the document.
|
62
62
|
|
63
63
|
|
@@ -77,7 +77,7 @@ Just install the gem:
|
|
77
77
|
|
78
78
|
## Real World Usage
|
79
79
|
|
80
|
-
The [`slideshow`](http://slideshow.rubyforge.org) (also known as Slide Show (S9))
|
80
|
+
The [`slideshow`](http://slideshow.rubyforge.org) gem (also known as Slide Show (S9))
|
81
81
|
that lets you create slide shows
|
82
82
|
and author slides in plain text using a wiki-style markup language that's easy-to-write and easy-to-read.
|
83
83
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
module TextUtils
|
3
|
+
module Filter
|
4
|
+
|
5
|
+
def code_block_curly_style( content, options={} )
|
6
|
+
# replace {{{ w/ <pre class='code'>
|
7
|
+
# replace }}} w/ </pre>
|
8
|
+
# use 4-6 { or } to escape back to literal value (e.g. {{{{ or {{{{{{ => {{{ )
|
9
|
+
# note: {{{ / }}} are anchored to beginning of line ( spaces and tabs before {{{/}}}allowed )
|
10
|
+
|
11
|
+
# track statistics
|
12
|
+
code_begin = 0
|
13
|
+
code_begin_esc = 0
|
14
|
+
code_end = 0
|
15
|
+
code_end_esc = 0
|
16
|
+
|
17
|
+
content.gsub!( /^[ \t]*(\{{3,6})/ ) do |match|
|
18
|
+
escaped = ($1.length > 3)
|
19
|
+
if escaped
|
20
|
+
code_begin_esc += 1
|
21
|
+
"{{{"
|
22
|
+
else
|
23
|
+
code_begin += 1
|
24
|
+
"<pre class='code'>"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
content.gsub!( /^[ \t]*(\}{3,6})/ ) do |match|
|
29
|
+
escaped = ($1.length > 3)
|
30
|
+
if escaped
|
31
|
+
code_end_esc += 1
|
32
|
+
"}}}"
|
33
|
+
else
|
34
|
+
code_end += 1
|
35
|
+
"</pre>"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
puts " Patching {{{/}}}-code blocks (#{code_begin}/#{code_end} blocks, " +
|
40
|
+
"#{code_begin_esc}/#{code_end_esc} escaped blocks)..."
|
41
|
+
|
42
|
+
content
|
43
|
+
end
|
44
|
+
|
45
|
+
end # module Filter
|
46
|
+
end # module TextUtils
|
@@ -0,0 +1,124 @@
|
|
1
|
+
|
2
|
+
module TextUtils
|
3
|
+
module Filter
|
4
|
+
|
5
|
+
def erb_django_style( content, options={} )
|
6
|
+
|
7
|
+
# replace expressions (support for single lines only)
|
8
|
+
# {{ expr }} -> <%= expr %>
|
9
|
+
# {% stmt %} -> <% stmt %> !! add in do if missing (for convenience)
|
10
|
+
#
|
11
|
+
# use use {{{ or {{{{ to escape expr back to literal value
|
12
|
+
# and use {%% %} to escape stmts
|
13
|
+
|
14
|
+
erb_expr = 0
|
15
|
+
erb_stmt_beg = 0
|
16
|
+
erb_stmt_end = 0
|
17
|
+
|
18
|
+
content.gsub!( /(\{{2,4})([^{}\n]+?)(\}{2,4})/ ) do |match|
|
19
|
+
escaped = ($1.length > 2)
|
20
|
+
if escaped
|
21
|
+
"{{#{$2}}}"
|
22
|
+
else
|
23
|
+
erb_expr += 1
|
24
|
+
"<%= #{erb_django_simple_params($2)} %>"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
content.gsub!( /(\{%{1,2})([ \t]*end[ \t]*)%\}/ ) do |match|
|
29
|
+
escaped = ($1.length > 2)
|
30
|
+
if escaped
|
31
|
+
"{%#{$2}%}"
|
32
|
+
else
|
33
|
+
erb_stmt_end += 1
|
34
|
+
"<% end %>"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
content.gsub!( /(\{%{1,2})([^%\n]+?)%\}/ ) do |match|
|
39
|
+
escaped = ($1.length > 2)
|
40
|
+
if escaped
|
41
|
+
"{%#{$2}%}"
|
42
|
+
else
|
43
|
+
erb_stmt_beg += 1
|
44
|
+
"<% #{erb_django_simple_params($2)} do %>"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
puts " Patching embedded Ruby (erb) code Django-style (#{erb_expr} {{-expressions," +
|
49
|
+
" #{erb_stmt_beg}/#{erb_stmt_end} {%-statements)..."
|
50
|
+
|
51
|
+
content
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
######################
|
57
|
+
## "private" helpers - do NOT use as filters - todo: add :nodoc: how?
|
58
|
+
|
59
|
+
def erb_django_simple_params( code )
|
60
|
+
|
61
|
+
# split into method/directive and parms plus convert params
|
62
|
+
code.sub!( /^[ \t]([\w.]+)(.*)/ ) do |match|
|
63
|
+
directive = $1
|
64
|
+
params = $2
|
65
|
+
|
66
|
+
"#{directive} #{params ? erb_simple_params(directive,params) : ''}"
|
67
|
+
end
|
68
|
+
|
69
|
+
code
|
70
|
+
end
|
71
|
+
|
72
|
+
def erb_simple_params( method, params )
|
73
|
+
|
74
|
+
# replace params to support html like attributes e.g.
|
75
|
+
# plus add comma separator
|
76
|
+
#
|
77
|
+
# class=part -> :class => 'part'
|
78
|
+
# 3rd/tutorial -> '3rd/tutorial'
|
79
|
+
# :css -> :css
|
80
|
+
|
81
|
+
return params if params.nil? || params.strip.empty?
|
82
|
+
|
83
|
+
params.strip!
|
84
|
+
## todo: add check for " ??
|
85
|
+
if params.include?( '=>' )
|
86
|
+
puts "** warning: skipping patching of params for helper '#{method}'; already includes '=>':"
|
87
|
+
puts " #{params}"
|
88
|
+
|
89
|
+
return params
|
90
|
+
end
|
91
|
+
|
92
|
+
before = params.clone
|
93
|
+
|
94
|
+
# 1) string-ify values and keys (that is, wrap in '')
|
95
|
+
# plus separate w/ commas
|
96
|
+
params.gsub!( /([:a-zA-Z0-9#][\w\/\-\.#()]*)|('[^'\n]*')/) do |match|
|
97
|
+
symbol = ( Regexp.last_match( 0 )[0,1] == ':' )
|
98
|
+
quoted = ( Regexp.last_match( 0 )[0,1] == "'" )
|
99
|
+
if symbol || quoted # return symbols or quoted string as is
|
100
|
+
"#{Regexp.last_match( 0 )},"
|
101
|
+
else
|
102
|
+
"'#{Regexp.last_match( 0 )}',"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# 2) symbol-ize hash keys
|
107
|
+
# change = to =>
|
108
|
+
# remove comma for key/value pairs
|
109
|
+
params.gsub!( /'(\w+)',[ \t]*=/ ) do |match|
|
110
|
+
":#{$1}=>"
|
111
|
+
end
|
112
|
+
|
113
|
+
# 3) remove trailing comma
|
114
|
+
params.sub!( /[ \t]*,[ \t]*$/, '' )
|
115
|
+
|
116
|
+
puts " Patching params for helper '#{method}' from '#{before}' to:"
|
117
|
+
puts " #{params}"
|
118
|
+
|
119
|
+
params
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
end # module Filter
|
124
|
+
end # module TextUtils
|
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
module TextUtils
|
3
|
+
module Filter
|
4
|
+
|
5
|
+
# allow plugins/helpers; process source (including header) using erb
|
6
|
+
def erb( content, options={} )
|
7
|
+
puts " Running embedded Ruby (erb) code/helpers..."
|
8
|
+
|
9
|
+
content = ERB.new( content ).result( binding() )
|
10
|
+
content
|
11
|
+
end
|
12
|
+
|
13
|
+
end # module Filter
|
14
|
+
end # module TextUtils
|
data/lib/textutils.rb
CHANGED
@@ -6,15 +6,19 @@ require 'pp'
|
|
6
6
|
require 'logger'
|
7
7
|
require 'optparse'
|
8
8
|
require 'fileutils'
|
9
|
+
require 'erb'
|
9
10
|
|
10
11
|
|
11
12
|
# our own code
|
12
13
|
|
14
|
+
require 'textutils/filter/code_filter'
|
13
15
|
require 'textutils/filter/comment_filter'
|
16
|
+
require 'textutils/filter/erb_django_filter'
|
17
|
+
require 'textutils/filter/erb_filter'
|
14
18
|
|
15
19
|
|
16
20
|
module TextUtils
|
17
21
|
|
18
|
-
VERSION = '0.
|
22
|
+
VERSION = '0.2.0'
|
19
23
|
|
20
24
|
end # module TextUtils
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: textutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gerald Bauer
|
@@ -61,7 +61,10 @@ files:
|
|
61
61
|
- README.markdown
|
62
62
|
- Rakefile
|
63
63
|
- lib/textutils.rb
|
64
|
+
- lib/textutils/filter/code_filter.rb
|
64
65
|
- lib/textutils/filter/comment_filter.rb
|
66
|
+
- lib/textutils/filter/erb_django_filter.rb
|
67
|
+
- lib/textutils/filter/erb_filter.rb
|
65
68
|
homepage: http://geraldb.github.com/textutils
|
66
69
|
licenses: []
|
67
70
|
|