textutils 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -16,6 +16,7 @@ lib/textutils/helper/title_helper.rb
16
16
  lib/textutils/helper/unicode_helper.rb
17
17
  lib/textutils/helper/value_helper.rb
18
18
  lib/textutils/helper/xml_helper.rb
19
+ lib/textutils/page.rb
19
20
  lib/textutils/patterns.rb
20
21
  lib/textutils/reader/code_reader.rb
21
22
  lib/textutils/reader/fixture_reader.rb
@@ -0,0 +1,197 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ ## page template class for book production w/ markdown
5
+ ## and static site compiler (e.g. jekyll)
6
+ ##
7
+ ## todo: move filters to filter for public reuse!!!
8
+
9
+ module TextUtils
10
+
11
+
12
+ ############
13
+ ## fix:
14
+ ### add some unit tests!!!!!!!!!!!!!!!!!
15
+ ###
16
+
17
+ class Page
18
+
19
+ ### convenience helper; use like:
20
+ ## Page.open() do |page|
21
+ ## page.write( text )
22
+ ## page.write( text )
23
+ ## end
24
+
25
+ def self.open( path, mode, opts={} )
26
+ page = self.new( path, mode, opts )
27
+ yield( page )
28
+ page.close
29
+ end
30
+
31
+ def self.create( path, opts={} )
32
+ ## todo: check if 'w' is good enough?? do NOT need to add +
33
+ page = self.new( path, 'w+', opts )
34
+ yield( page )
35
+ page.close
36
+ end
37
+
38
+ def self.update( path, opts={} )
39
+ ## todo: check if 'a' is good enough?? do NOT need to add +
40
+ page = self.new( path, 'a+', opts )
41
+ yield( page )
42
+ page.close
43
+ end
44
+
45
+
46
+ def initialize( path, mode, opts={} )
47
+ ## check if folders exists? if not create folder in path
48
+ FileUtils.mkdir_p( File.dirname(path) )
49
+
50
+ @file = File.new( path, mode )
51
+
52
+ ## add frontmatter if passed in
53
+ ## todo: assert check if mode = 'w' and NOT 'a' !!!
54
+ @file.write render_frontmatter( opts[:frontmatter] ) if opts[:frontmatter]
55
+ end
56
+
57
+ def write( text )
58
+ @file.write( text )
59
+ end
60
+
61
+ def close
62
+ @file.close
63
+ end
64
+
65
+ private
66
+
67
+ ###########################
68
+ # helpers
69
+ # - make public for reuse !!!!!
70
+
71
+ def render_frontmatter( h )
72
+ buf = ''
73
+ buf += "---\n"
74
+
75
+ h.each do |key,value|
76
+ buf += "#{key}: #{value}\n"
77
+ end
78
+
79
+ buf += "---\n\n"
80
+ buf
81
+ end
82
+
83
+ end # class Page
84
+
85
+
86
+ class PageTemplate
87
+
88
+
89
+ ###
90
+ ## todo: what is the best convention for loading file and handling string
91
+ ## for now it its:
92
+ #
93
+ # PageTemplate.read( 'to/path ' ) or --- use load ???? instead of read??
94
+ # PageTemplate.new( 'template content' )
95
+
96
+
97
+ def self.read( path )
98
+ self.new( File.read_utf8( path ) )
99
+ end
100
+
101
+
102
+ def initialize( tmpl )
103
+ @tmpl = tmpl.dup # make a copy; just to be sure no one will change text
104
+ end
105
+
106
+ def render( ctx )
107
+ # note: erb offers the following trim modes:
108
+ # 1) <> omit newline for lines starting with <% and ending in %>
109
+ # 2) > omit newline for lines ending in %>
110
+ # 3) omit blank lines ending in -%>
111
+ ## run filters
112
+ tmpl = remove_html_comments( @tmpl )
113
+ tmpl = remove_blanks( tmpl )
114
+
115
+ tmpl = django_to_erb( tmpl ) ## allow django/jinja style templates
116
+
117
+ tmpl = remove_leading_spaces( tmpl )
118
+ tmpl = concat_lines( tmpl )
119
+
120
+ text = ERB.new( tmpl, nil, '<>' ).result( ctx )
121
+
122
+ ### text = cleanup_newlines( text )
123
+ text
124
+ end
125
+
126
+ #######################
127
+ # filters
128
+ # - use better names and make public for reuse!!!!
129
+
130
+ def django_to_erb( text )
131
+ ## convert django style markers to erb style marker e.g
132
+ # {% %} becomes <% %> -- supports multi-line
133
+ # {{ }} becomes <%= %> - does NOT support multi-line
134
+
135
+ ## comments (support multi-line)
136
+ text = text.gsub( /\{#(.+?)#\}/m ) do |_|
137
+ "<%# #{1} %>"
138
+ end
139
+
140
+ text = text.gsub( /\{%(.+?)%\}/m ) do |_|
141
+ ## note: also replace newlines w/ %>\n<% to split
142
+ # multi-line stmts into single-line stmts
143
+ # lets us use
144
+ # {%
145
+ # %} will become
146
+ # <% %>
147
+ # <% %>
148
+ "<% #{$1} %>".gsub( "\n", " %>\n<% " )
149
+ end
150
+
151
+ # note: for now {{ }} will NOT support multi-line
152
+ text = text.gsub( /\{\{(.+?)\}\}/ ) do |_|
153
+ "<%= #{$1} %>"
154
+ end
155
+
156
+ text
157
+ end
158
+
159
+ def remove_html_comments( text )
160
+ text.gsub( /<!--.+?-->/, '' )
161
+ end
162
+
163
+ def remove_leading_spaces( text )
164
+ # remove leading spaces if less than four !!!
165
+ text.gsub( /^[ \t]+(?![ \t])/, '' ) # use negative regex lookahead e.g. (?!)
166
+ end
167
+
168
+ def remove_blanks( text )
169
+ # remove lines only with ..
170
+ text.gsub( /^[ \t]*\.{2}[ \t]*\n/, '' )
171
+ end
172
+
173
+ def cleanup_newlines( text )
174
+ # remove all blank lines that go over three
175
+ text.gsub( /\n{4,}/, "\n\n\n" )
176
+ end
177
+
178
+
179
+ def concat_lines( text )
180
+ # lines ending with ++ will get newlines get removed
181
+ # e.g.
182
+ # >| hello1 ++
183
+ # >1 hello2
184
+ # becomes
185
+ # >| hello1 hello2
186
+
187
+ #
188
+ # note: do NOT use \s - will include \n (newline) ??
189
+
190
+ text.gsub( /[ \t]+\+{2}[ \t]*\n[ \t]*/, ' ' ) # note: replace with single space
191
+ end
192
+
193
+
194
+ end # class PageTemplate
195
+
196
+ end # module TextUtils
197
+
@@ -1,7 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
-
4
-
5
3
  module TextUtils
6
4
  # make helpers available as class methods e.g. TextUtils.convert_unicode_dashes_to_plain_ascii
7
5
  extend UnicodeHelper
@@ -33,7 +31,7 @@ end
33
31
 
34
32
 
35
33
  def find_data_path_from_gemfile_gitref( name )
36
- puts "[debug] find_data_path( name='#{name}' )..."
34
+ puts "[textutils] find_data_path( name='#{name}' )..."
37
35
  puts "load path:"
38
36
  pp $LOAD_PATH
39
37
 
@@ -1,7 +1,8 @@
1
1
 
2
2
  module TextUtils
3
3
 
4
- VERSION = '0.8.4'
4
+ VERSION = '0.8.5'
5
5
 
6
6
  end # module TextUtils
7
7
 
8
+
data/lib/textutils.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
 
2
4
  # core and stlibs
3
5
 
@@ -43,3 +45,22 @@ require 'textutils/reader/fixture_reader'
43
45
 
44
46
  require 'textutils/classifier'
45
47
 
48
+ require 'textutils/page' # for book pages and page templates
49
+
50
+
51
+
52
+ module TextUtils
53
+
54
+ def self.banner
55
+ "textutils/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
56
+ end
57
+
58
+ def self.root
59
+ "#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
60
+ end
61
+
62
+ end
63
+
64
+
65
+
66
+ puts TextUtils.banner if $DEBUG # say hello
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: textutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-12 00:00:00.000000000 Z
12
+ date: 2014-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: logutils
16
- requirement: &20379624 !ruby/object:Gem::Requirement
16
+ requirement: &21250380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.5'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *20379624
24
+ version_requirements: *21250380
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rdoc
27
- requirement: &20377728 !ruby/object:Gem::Requirement
27
+ requirement: &21250032 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '4.0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *20377728
35
+ version_requirements: *21250032
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: hoe
38
- requirement: &20376972 !ruby/object:Gem::Requirement
38
+ requirement: &21249564 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '3.7'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *20376972
46
+ version_requirements: *21249564
47
47
  description: textutils - Text Filters, Helpers, Readers and More
48
48
  email: ruby-talk@ruby-lang.org
49
49
  executables: []
@@ -71,6 +71,7 @@ files:
71
71
  - lib/textutils/helper/unicode_helper.rb
72
72
  - lib/textutils/helper/value_helper.rb
73
73
  - lib/textutils/helper/xml_helper.rb
74
+ - lib/textutils/page.rb
74
75
  - lib/textutils/patterns.rb
75
76
  - lib/textutils/reader/code_reader.rb
76
77
  - lib/textutils/reader/fixture_reader.rb