preproc 0.0.1 → 0.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 +4 -4
- data/.gemtest +0 -0
- data/lib/preproc.rb +100 -0
- data/lib/preproc/version.rb +2 -2
- data/test/test_incl.rb +24 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 010e8983d1847ac0c76197c74740339c3a8d8242
|
4
|
+
data.tar.gz: d219dec85408bcaffb072393780bc8eed62af109
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf7a60481e2fd36a650c4c4ef8816449bd36388fd74a0faa4838b2dfab6d3fa84ee8ec7fecbc09b700867320f26d3ddae8610cd81639bbd94f4440ba7567b8bf
|
7
|
+
data.tar.gz: 5476cacabfac6632042ed5c02a4d6259c8bad143a3f845525fee70b0faa754286ceefcdac90b7e90c81cbedeb8e9f46cfbee16c1fa6607560acdffa1c183b1e5
|
data/.gemtest
ADDED
File without changes
|
data/lib/preproc.rb
CHANGED
@@ -10,6 +10,106 @@ require 'fetcher'
|
|
10
10
|
require 'preproc/version' # let version always go first
|
11
11
|
|
12
12
|
|
13
|
+
|
14
|
+
module Preproc
|
15
|
+
|
16
|
+
#############
|
17
|
+
### todo/fix: use/find better name? - e.g. Fetcher.read_utf8!() move out of global ns etc.
|
18
|
+
## move to Fetcher gem; (re)use!!! remove here
|
19
|
+
### e.g. use Fetch.read_utf8!() e.g. throws exception on error
|
20
|
+
## read_blob!()
|
21
|
+
def self.fetcher_read_utf8!( src )
|
22
|
+
worker = Fetcher::Worker.new
|
23
|
+
res = worker.get_response( src )
|
24
|
+
if res.code != '200'
|
25
|
+
puts "sorry; failed to fetch >#{src} - HTTP #{res.code} - #{res.message}"
|
26
|
+
exit 1
|
27
|
+
end
|
28
|
+
|
29
|
+
###
|
30
|
+
# Note: Net::HTTP will NOT set encoding UTF-8 etc.
|
31
|
+
# will be set to ASCII-8BIT == BINARY == Encoding Unknown; Raw Bytes Here
|
32
|
+
# thus, set/force encoding to utf-8
|
33
|
+
txt = res.body.to_s
|
34
|
+
txt = txt.force_encoding( Encoding::UTF_8 )
|
35
|
+
txt
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
class IncludeReader
|
40
|
+
|
41
|
+
###
|
42
|
+
## concat files w/ @include directive
|
43
|
+
include LogUtils::Logging
|
44
|
+
|
45
|
+
def self.from_url( src )
|
46
|
+
uri = URI.parse( src )
|
47
|
+
text = Preproc.fetcher_read_utf8!( src )
|
48
|
+
|
49
|
+
base_path = uri.path[0..uri.path.rindex('/')] # get everything upto incl. last slash (/)
|
50
|
+
self.new( text, base: "#{uri.scheme}://#{uri.host}:#{uri.port}#{base_path}" )
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def initialize( text, opts={} )
|
55
|
+
@text = text
|
56
|
+
@base = opts[:base]
|
57
|
+
|
58
|
+
logger.debug(" base: #{@base}")
|
59
|
+
end
|
60
|
+
|
61
|
+
def read
|
62
|
+
preproc( @text )
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
private
|
67
|
+
def preproc( text )
|
68
|
+
buf = ''
|
69
|
+
text.each_line do |line|
|
70
|
+
### e.g. allows
|
71
|
+
## @include 'test' or
|
72
|
+
## @INCLUDE "test"
|
73
|
+
## note: for now we strip (allow) leading and trailing spaces (e.g. [ ]*)
|
74
|
+
##
|
75
|
+
## todo/allow
|
76
|
+
## @include( test ) - alternative syntax - why? why not?
|
77
|
+
if line =~ /^[ ]*@include[ ]+("|')(.+?)\1[ ]*$/i
|
78
|
+
logger.info( " try to include '#{$2}'")
|
79
|
+
## buf << preproc( 'include here' )
|
80
|
+
buf << preproc( read_include( $2 ) )
|
81
|
+
buf << "\n"
|
82
|
+
else
|
83
|
+
buf << line
|
84
|
+
buf << "\n"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
buf
|
88
|
+
end
|
89
|
+
|
90
|
+
def read_include( name )
|
91
|
+
## for now assume fetch via url
|
92
|
+
## note: assume partial e.g. add leading underscore (_)
|
93
|
+
## e.g. test => _test.ini
|
94
|
+
|
95
|
+
src = "#{@base}_#{name}.ini"
|
96
|
+
logger.info( " try to fetch include #{src}")
|
97
|
+
|
98
|
+
Preproc.fetcher_read_utf8!( src )
|
99
|
+
end
|
100
|
+
|
101
|
+
end # class IncludeReader
|
102
|
+
|
103
|
+
|
104
|
+
end # module Preproc
|
105
|
+
|
106
|
+
|
107
|
+
## add top level (convenience) alias
|
108
|
+
InclPreproc = Preproc::IncludeReader
|
109
|
+
IncludePreproc = Preproc::IncludeReader
|
110
|
+
|
111
|
+
|
112
|
+
|
13
113
|
## say hello
|
14
114
|
puts Preproc.banner if $DEBUG || (defined?($RUBYLIBS_DEBUG) && $RUBYLIBS_DEBUG)
|
15
115
|
|
data/lib/preproc/version.rb
CHANGED
data/test/test_incl.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_incl.rb
|
6
|
+
# or better
|
7
|
+
# rake test
|
8
|
+
|
9
|
+
|
10
|
+
require 'helper'
|
11
|
+
|
12
|
+
|
13
|
+
class TestIncl < MiniTest::Test
|
14
|
+
|
15
|
+
def test_ruby
|
16
|
+
src = 'https://raw.github.com/feedreader/planet-ruby/master/ruby.ini'
|
17
|
+
txt = InclPreproc.from_url( src ).read
|
18
|
+
pp txt
|
19
|
+
|
20
|
+
assert true ## if we get here it should work
|
21
|
+
end
|
22
|
+
|
23
|
+
end # class TestIncl
|
24
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: preproc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
@@ -76,12 +76,14 @@ extra_rdoc_files:
|
|
76
76
|
- Manifest.txt
|
77
77
|
- README.md
|
78
78
|
files:
|
79
|
+
- ".gemtest"
|
79
80
|
- HISTORY.md
|
80
81
|
- Manifest.txt
|
81
82
|
- README.md
|
82
83
|
- Rakefile
|
83
84
|
- lib/preproc.rb
|
84
85
|
- lib/preproc/version.rb
|
86
|
+
- test/test_incl.rb
|
85
87
|
homepage: https://github.com/rubylibs/preproc
|
86
88
|
licenses:
|
87
89
|
- Public Domain
|
@@ -108,4 +110,5 @@ rubygems_version: 2.4.2
|
|
108
110
|
signing_key:
|
109
111
|
specification_version: 4
|
110
112
|
summary: preproc - simple preprocessor (lets you include files in files in files etc.)
|
111
|
-
test_files:
|
113
|
+
test_files:
|
114
|
+
- test/test_incl.rb
|