widow 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/widow +127 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4010c817291eb968a10b0e7f1b1488059053ce00
4
+ data.tar.gz: 78ed7079adf9ee35232090e4e7f4bfaede354cae
5
+ SHA512:
6
+ metadata.gz: 19eb4c34bac3babd6b5b3dfa9d7f5e560e4fce20a0370ac2f2ac15cccfcb92ad67b33a4f70131fff79748fa684a252f77cd198ae0649ec8fe1fbdd9721062783
7
+ data.tar.gz: 36168260e0bd50728f3aa73faa3872a172b1c0a7c841899e165a7ec3b971d96395b39dd20b8b9b946bc7aec43b86e5c99ef41688b3404abfdea4dd49cccca8aa
data/bin/widow ADDED
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'tilt'
4
+ require 'find'
5
+ require 'pathname'
6
+ require 'fileutils'
7
+ require 'rack'
8
+
9
+ class Widow
10
+ @@opt_parser = OptionParser.new do |parser|
11
+ end
12
+
13
+ def initialize args = {}
14
+ opts = args[:opts]
15
+ return if opts.empty?
16
+
17
+ case opts.first
18
+ when /cut/
19
+ if opts[1]
20
+ cut *opts[1].split(':')
21
+ else
22
+ cut
23
+ end
24
+ when /spin/
25
+ if opts[1]
26
+ spin *opts[1].split(':')
27
+ else
28
+ cut
29
+ end
30
+ end
31
+ end
32
+
33
+ def cut from = Dir.pwd, to = Dir.pwd
34
+ puts from = Pathname.new(from)
35
+ puts to = Pathname.new(to)
36
+ Find.find from do |path|
37
+ path = Pathname.new path
38
+ next if path == from
39
+ make from, path.relative_path_from(from), to
40
+ end
41
+ end
42
+
43
+ def spin src = Dir.pwd, bin = Dir.pwd
44
+ src = Pathname.new src
45
+ bin = Pathname.new bin
46
+
47
+ allowed_templates = Tilt.lazy_map.map do |ext, _|
48
+ begin
49
+ Tilt[ext]
50
+ next ext
51
+ rescue LoadError
52
+ next nil
53
+ end
54
+ end.compact
55
+
56
+ Rack::Handler.default.run(
57
+ Proc.new do |env|
58
+ name = Pathname.new env["PATH_INFO"][1..-1]
59
+ file = bin + name
60
+ original = src + name
61
+ puts original
62
+ unless original.exist?
63
+ matching_templates = allowed_templates.select do |ext|
64
+ next Tilt[ext].metadata[:mime_type] == case name.extname[1..-1]
65
+ when 'js' then 'application/javascript'
66
+ when 'css' then 'text/css'
67
+ when 'html' then 'text/html'
68
+ else nil
69
+ end
70
+ end
71
+ original = Pathname.glob(src + "#{name.basename name.extname}.{#{matching_templates.join ?,}}").first
72
+ end
73
+ puts original
74
+ next [404, {'Content-Type' => 'text/html'}, ["File not found!"]] if original.nil? || !original.exist?
75
+
76
+ begin
77
+ make src, original.relative_path_from(src), bin if !file.exist? || file.mtime < original.mtime
78
+ next [200, {'Content-Type' => 'text/html'}, [file.file? ? file.read : '']]
79
+ rescue RuntimeError, LoadError => e
80
+ next [502, {'Content-Type' => 'text/html'}, [e.message]]
81
+ end
82
+ end,
83
+ Port: 8888
84
+ )
85
+ end
86
+
87
+ private
88
+ # proxies or renders a file based on it's properties and tilt capacity
89
+ def make root, file, to
90
+ unless Tilt.registered? file.extname[1..-1].to_s
91
+ proxy root, file, to
92
+ return
93
+ end
94
+
95
+ begin
96
+ render root, file, to
97
+ rescue RuntimeError, LoadError => e
98
+ puts e.message
99
+ proxy root, file, to
100
+ end
101
+ end
102
+
103
+ # creates a carbon copy of a file elsewhere
104
+ def proxy root, file, to
105
+ to.mkpath unless to.exist?
106
+ obj = to + file
107
+ src = root + file
108
+ if src.directory?
109
+ obj.mkpath unless obj.exist?
110
+ elsif src.file?
111
+ FileUtils.cp src, obj
112
+ end
113
+ end
114
+
115
+ # Takes a file and tries to render it through tilt to the destination directory
116
+ def render root, file, to
117
+ tilt = Tilt.new root + file
118
+ type = case tilt.metadata[:mime_type]
119
+ when 'application/javascript' then 'js'
120
+ when 'text/css' then 'css'
121
+ else 'html'
122
+ end
123
+ File.write to + "#{file.basename file.extname}.#{type}", tilt.render
124
+ end
125
+ end
126
+
127
+ Widow.new opts: ARGV
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: widow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Matveev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables:
16
+ - widow
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/widow
21
+ homepage:
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.6.8
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Spins web superlanguages as you write!
45
+ test_files: []