dwt 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,7 +1,5 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
2
+ gem "trollop", ">= 0"
5
3
 
6
4
  # Add dependencies to develop your gem here.
7
5
  # Include everything needed to run rake, tests, features, etc.
@@ -9,6 +9,7 @@ GEM
9
9
  rake (0.9.2)
10
10
  rcov (0.9.9)
11
11
  shoulda (2.11.3)
12
+ trollop (1.16.2)
12
13
 
13
14
  PLATFORMS
14
15
  ruby
@@ -18,3 +19,4 @@ DEPENDENCIES
18
19
  jeweler (~> 1.6.4)
19
20
  rcov
20
21
  shoulda
22
+ trollop
@@ -1,6 +1,6 @@
1
1
  = dwt
2
2
 
3
- Description goes here.
3
+ Library and command line tool to apply Dreamweaver templates to HTML files.
4
4
 
5
5
  == Contributing to dwt
6
6
 
data/Rakefile CHANGED
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
18
18
  gem.homepage = "http://github.com/andrewferk/dwt"
19
19
  gem.license = "MIT"
20
20
  gem.summary = 'Dreamweaver Template tool'
21
- gem.description = 'Dreamweaver Template tool'
21
+ gem.description = 'Library and command line tool to apply Dreamweaver templates to HTML files.'
22
22
  gem.email = "andrewferk@gmail.com"
23
23
  gem.authors = ["Andrew Ferk"]
24
24
  # dependencies defined in Gemfile
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/bin/dwt ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require 'trollop'
5
+ require 'dwt'
6
+
7
+ opts = Trollop::options do
8
+ opt :template, "DWT file", :short => 't', :type => String
9
+ opt :file, "Target file", :short => 'f', :type => String
10
+ end
11
+ Trollop::die :template, "must be present" unless opts[:template]
12
+ Trollop::die :template, "must exist" unless File.exist?(opts[:template]) if opts[:template]
13
+ Trollop::die :file, "must be present" unless opts[:file]
14
+ Trollop::die :file, "must exist" unless File.exist?(opts[:template]) if opts[:template]
15
+
16
+ dwt = DWT.new
17
+ dwt.template = opts[:template]
18
+ dwt.files = opts[:file]
19
+ puts dwt.apply
20
+
21
+ exit 0
@@ -5,13 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dwt}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Andrew Ferk}]
12
- s.date = %q{2011-07-31}
13
- s.description = %q{Dreamweaver Template tool}
12
+ s.date = %q{2011-08-01}
13
+ s.description = %q{Library and command line tool to apply Dreamweaver templates to HTML files.}
14
14
  s.email = %q{andrewferk@gmail.com}
15
+ s.executables = [%q{dwt}]
15
16
  s.extra_rdoc_files = [
16
17
  "LICENSE.txt",
17
18
  "README.rdoc"
@@ -24,12 +25,19 @@ Gem::Specification.new do |s|
24
25
  "README.rdoc",
25
26
  "Rakefile",
26
27
  "VERSION",
28
+ "bin/dwt",
27
29
  "dwt.gemspec",
28
- "dwt.rb",
29
30
  "lib/dwt.rb",
30
- "test/files/simple-01.dwt",
31
+ "test/files/Templates/link-attribute-01.dwt",
32
+ "test/files/Templates/link-attribute-02.dwt",
33
+ "test/files/Templates/link-attribute-03.dwt",
34
+ "test/files/Templates/simple-01.dwt",
35
+ "test/files/Templates/simple-02.dwt",
36
+ "test/files/link-attribute-01.html",
37
+ "test/files/link-attribute-02.html",
38
+ "test/files/link-attribute-03.html",
39
+ "test/files/outside-html-01.html",
31
40
  "test/files/simple-01.html",
32
- "test/files/simple-02.dwt",
33
41
  "test/files/simple-02.html",
34
42
  "test/helper.rb",
35
43
  "test/test_dwt.rb"
@@ -44,17 +52,20 @@ Gem::Specification.new do |s|
44
52
  s.specification_version = 3
45
53
 
46
54
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
+ s.add_runtime_dependency(%q<trollop>, [">= 0"])
47
56
  s.add_development_dependency(%q<shoulda>, [">= 0"])
48
57
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
58
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
50
59
  s.add_development_dependency(%q<rcov>, [">= 0"])
51
60
  else
61
+ s.add_dependency(%q<trollop>, [">= 0"])
52
62
  s.add_dependency(%q<shoulda>, [">= 0"])
53
63
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
64
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
55
65
  s.add_dependency(%q<rcov>, [">= 0"])
56
66
  end
57
67
  else
68
+ s.add_dependency(%q<trollop>, [">= 0"])
58
69
  s.add_dependency(%q<shoulda>, [">= 0"])
59
70
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
71
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
data/lib/dwt.rb CHANGED
@@ -16,14 +16,19 @@ class DWT
16
16
  copy = (tpl_end ? tpl[0,tpl_end] : tpl)
17
17
  copy.gsub!(/<html([^>]*)>/, '<html\1><!-- InstanceBegin template="'+tpl_src+'" codeOutsideHTMLIsLocked="false" -->')
18
18
  copy.gsub!(/<\/html>/,'<!-- InstanceEnd --></html>')
19
- tpl_copy << copy
19
+ html_index = copy.index('<html')
20
+ tpl_copy << (html_index ? copy[html_index, copy.length - html_index] : copy)
20
21
  tpl = tpl_start ? tpl[tpl_start,tpl.length] : ''
21
22
  end while tpl.length > 0
22
23
 
23
24
  tar_end_editable_str = "<!-- InstanceEndEditable -->"
24
- tar = File.open(files[0]).read
25
+ filename = files[0]
26
+ tar = File.open(filename).read
25
27
  tar_copy = []
26
28
 
29
+ html_index = tar.index('<html')
30
+ tar_copy << tar[0, html_index]
31
+
27
32
  begin
28
33
  tar_start = tar.index("<!-- InstanceBeginEditable")
29
34
  tar_end = tar.index(tar_end_editable_str)
@@ -34,10 +39,40 @@ class DWT
34
39
 
35
40
  new = ''
36
41
  (0..(tar_copy.length)).to_a.each do |i|
37
- new = new += tpl_copy[i]
38
- new = new += tar_copy[i] if tar_copy[i]
42
+ next if tar_copy[i].nil?
43
+ new = new += tar_copy[i]
44
+ copy = tpl_copy[i]
45
+ copy = apply_link_update(copy,'link','href',filename)
46
+ copy = apply_link_update(copy,'script','src',filename)
47
+ copy = apply_link_update(copy,'form','action',filename)
48
+ copy = apply_link_update(copy,'a','href',filename)
49
+ copy = apply_link_update(copy,'img','src',filename)
50
+ new = new += copy
39
51
  end
40
52
 
41
53
  return new
42
54
  end
55
+
56
+ private
57
+ def apply_link_update(html, tag, attribute, filename)
58
+ html.gsub(/<#{tag}(.*)#{attribute}="([^"]*)"([^>]*)>/) do |match|
59
+ tpl_link = $2
60
+ if tpl_link.start_with?('http')
61
+ replace_link = tpl_link
62
+ else
63
+ tpl_links = tpl_link.split(File::SEPARATOR)
64
+ tpl_links.delete_at(0) if tpl_links[0] == '..'
65
+ filedir = Dir.new(File.dirname(filename))
66
+ back_separators = 0
67
+ begin
68
+ filedir_ls = filedir.each.to_a
69
+ break if filedir_ls.include?('Templates')
70
+ filedir.chdir("..")
71
+ back_separators += 1
72
+ end while filedir.pwd != "/"
73
+ replace_link = ("../" * back_separators) + tpl_links.join('/')
74
+ end
75
+ "<#{tag}#{$1}#{attribute}=\"#{replace_link}\"#{$3}>"
76
+ end
77
+ end
43
78
  end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <!-- TemplateBeginEditable name="doctitle" -->
5
+ <title>Untitled Document</title>
6
+ <!-- TemplateEndEditable -->
7
+ <link href="../styles/main.css" rel="stylesheet" type="text/css" media="screen" />
8
+ <script type="text/javascript" src="../javascripts/app.js"></script>
9
+ </head>
10
+ <body>
11
+ <form action="../search" method="get" name="search">
12
+ </form>
13
+ <a href="../home" title="Home">Home</a>
14
+ <img src="../images/logo.png" alt="Logo" />
15
+ </body>
16
+ </html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <!-- TemplateBeginEditable name="doctitle" -->
5
+ <title>Untitled Document</title>
6
+ <!-- TemplateEndEditable -->
7
+ </head>
8
+ <body>
9
+ <a href="http://www.andrewferk.com/" title="Andrew Ferk">Andrew Ferk</a>
10
+ </body>
11
+ </html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <!-- TemplateBeginEditable name="doctitle" -->
5
+ <title>Untitled Document</title>
6
+ <!-- TemplateEndEditable -->
7
+ <script type="text/javascript" src="app.js"></script>
8
+ </head>
9
+ <body>
10
+ </body>
11
+ </html>
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html><!-- InstanceBegin template="/Templates/link-attribute-01.dwt" codeOutsideHTMLIsLocked="false" -->
3
+ <head>
4
+ <!-- InstanceBeginEditable name="doctitle" -->
5
+ <title>Untitled Document</title>
6
+ <!-- InstanceEndEditable -->
7
+ <link href="styles/main.css" rel="stylesheet" type="text/css" media="screen" />
8
+ <script type="text/javascript" src="javascripts/app.js"></script>
9
+ </head>
10
+ <body>
11
+ <form action="search" method="get" name="search">
12
+ </form>
13
+ <a href="home" title="Home">Home</a>
14
+ <img src="images/logo.png" alt="Logo" />
15
+ </body>
16
+ <!-- InstanceEnd --></html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html><!-- InstanceBegin template="/Templates/link-attribute-02.dwt" codeOutsideHTMLIsLocked="false" -->
3
+ <head>
4
+ <!-- InstanceBeginEditable name="doctitle" -->
5
+ <title>Untitled Document</title>
6
+ <!-- InstanceEndEditable -->
7
+ </head>
8
+ <body>
9
+ <a href="http://www.andrewferk.com/" title="Andrew Ferk">Andrew Ferk</a>
10
+ </body>
11
+ <!-- InstanceEnd --></html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html><!-- InstanceBegin template="/Templates/link-attribute-03.dwt" codeOutsideHTMLIsLocked="false" -->
3
+ <head>
4
+ <!-- InstanceBeginEditable name="doctitle" -->
5
+ <title>Untitled Document</title>
6
+ <!-- InstanceEndEditable -->
7
+ <script type="text/javascript" src="app.js"></script>
8
+ </head>
9
+ <body>
10
+ </body>
11
+ <!-- InstanceEnd --></html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <!-- This comment is important -->
3
+ <html><!-- InstanceBegin template="/Templates/simple-01.dwt" codeOutsideHTMLIsLocked="false" -->
4
+ <head>
5
+ <!-- InstanceBeginEditable name="doctitle" -->
6
+ <title>Untitled Document</title>
7
+ <!-- InstanceEndEditable -->
8
+ </head>
9
+ <body>
10
+ </body>
11
+ <!-- InstanceEnd --></html>
@@ -1,4 +1,4 @@
1
- <!DOCCTYPE html>
1
+ <!DOCTYPE html>
2
2
  <html><!-- InstanceBegin template="/Templates/simple-01.dwt" codeOutsideHTMLIsLocked="false" -->
3
3
  <head>
4
4
  <!-- InstanceBeginEditable name="doctitle" -->
@@ -2,9 +2,41 @@ require 'helper'
2
2
 
3
3
  class TestDwt < Test::Unit::TestCase
4
4
  should "update page with template" do
5
- dwt = DWT.new
6
- dwt.template = File.dirname(__FILE__) + '/files/simple-02.dwt'
7
- dwt.files = File.dirname(__FILE__) + '/files/simple-01.html'
8
- assert_equal File.open(File.dirname(__FILE__) + '/files/simple-02.html').read, dwt.apply
5
+ assert_dwt(File.dirname(__FILE__) + '/files/Templates/simple-02.dwt',
6
+ File.dirname(__FILE__) + '/files/simple-01.html',
7
+ File.open(File.dirname(__FILE__) + '/files/simple-02.html').read)
9
8
  end
9
+
10
+ should "not touch coude outside html tag" do
11
+ assert_dwt(File.dirname(__FILE__) + '/files/Templates/simple-01.dwt',
12
+ File.dirname(__FILE__) + '/files/outside-html-01.html',
13
+ File.open(File.dirname(__FILE__) + '/files/outside-html-01.html').read)
14
+ end
15
+
16
+ should "modify href, src, action attributes" do
17
+ assert_dwt(File.dirname(__FILE__) + '/files/Templates/link-attribute-01.dwt',
18
+ File.dirname(__FILE__) + '/files/simple-01.html',
19
+ File.open(File.dirname(__FILE__) + '/files/link-attribute-01.html').read)
20
+ end
21
+
22
+ should "modify link attributes, but ignore external location" do
23
+ assert_dwt(File.dirname(__FILE__) + '/files/Templates/link-attribute-02.dwt',
24
+ File.dirname(__FILE__) + '/files/simple-01.html',
25
+ File.open(File.dirname(__FILE__) + '/files/link-attribute-02.html').read)
26
+ end
27
+
28
+ should "modify link attributes, but ignore external not relative" do
29
+ assert_dwt(File.dirname(__FILE__) + '/files/Templates/link-attribute-03.dwt',
30
+ File.dirname(__FILE__) + '/files/simple-01.html',
31
+ File.open(File.dirname(__FILE__) + '/files/link-attribute-03.html').read)
32
+ end
33
+
34
+ private
35
+ def assert_dwt(template, file, expected)
36
+ dwt = DWT.new
37
+ dwt.template = template
38
+ dwt.files = file
39
+ assert_equal expected, dwt.apply
40
+ end
41
+
10
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dwt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-31 00:00:00.000000000Z
12
+ date: 2011-08-01 00:00:00.000000000Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: trollop
16
+ requirement: &13301780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *13301780
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: shoulda
16
- requirement: &24127240 !ruby/object:Gem::Requirement
27
+ requirement: &13299940 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ! '>='
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: '0'
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *24127240
35
+ version_requirements: *13299940
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: bundler
27
- requirement: &24126420 !ruby/object:Gem::Requirement
38
+ requirement: &13298240 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: 1.0.0
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *24126420
46
+ version_requirements: *13298240
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: jeweler
38
- requirement: &24122600 !ruby/object:Gem::Requirement
49
+ requirement: &13297300 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 1.6.4
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *24122600
57
+ version_requirements: *13297300
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rcov
49
- requirement: &24121760 !ruby/object:Gem::Requirement
60
+ requirement: &13296440 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,12 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *24121760
58
- description: Dreamweaver Template tool
68
+ version_requirements: *13296440
69
+ description: Library and command line tool to apply Dreamweaver templates to HTML
70
+ files.
59
71
  email: andrewferk@gmail.com
60
- executables: []
72
+ executables:
73
+ - dwt
61
74
  extensions: []
62
75
  extra_rdoc_files:
63
76
  - LICENSE.txt
@@ -70,12 +83,19 @@ files:
70
83
  - README.rdoc
71
84
  - Rakefile
72
85
  - VERSION
86
+ - bin/dwt
73
87
  - dwt.gemspec
74
- - dwt.rb
75
88
  - lib/dwt.rb
76
- - test/files/simple-01.dwt
89
+ - test/files/Templates/link-attribute-01.dwt
90
+ - test/files/Templates/link-attribute-02.dwt
91
+ - test/files/Templates/link-attribute-03.dwt
92
+ - test/files/Templates/simple-01.dwt
93
+ - test/files/Templates/simple-02.dwt
94
+ - test/files/link-attribute-01.html
95
+ - test/files/link-attribute-02.html
96
+ - test/files/link-attribute-03.html
97
+ - test/files/outside-html-01.html
77
98
  - test/files/simple-01.html
78
- - test/files/simple-02.dwt
79
99
  - test/files/simple-02.html
80
100
  - test/helper.rb
81
101
  - test/test_dwt.rb
@@ -94,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
114
  version: '0'
95
115
  segments:
96
116
  - 0
97
- hash: -1990586235474243930
117
+ hash: 3986656619842453243
98
118
  required_rubygems_version: !ruby/object:Gem::Requirement
99
119
  none: false
100
120
  requirements:
data/dwt.rb DELETED
@@ -1,42 +0,0 @@
1
- require 'trollop'
2
-
3
- opts = Trollop::options do
4
- opt :template, "DWT file", :short => 't', :type => String
5
- opt :file, "Target file", :short => 'f', :type => String
6
- end
7
- Trollop::die :template, "must be present" unless opts[:template]
8
- Trollop::die :template, "must exist" unless File.exist?(opts[:template]) if opts[:template]
9
- Trollop::die :file, "must be present" unless opts[:file]
10
- Trollop::die :file, "must exist" unless File.exist?(opts[:template]) if opts[:template]
11
-
12
- tpl_end_editable_str = "<!-- TemplateEndEditable -->"
13
- tpl = File.open(opts[:template]).read
14
- tpl_copy = []
15
-
16
- begin
17
- tpl_end = tpl.index("<!-- TemplateBeginEditable")
18
- tpl_start = tpl.index(tpl_end_editable_str)
19
- tpl_start = tpl_start + tpl_end_editable_str.length if tpl_start
20
- tpl_copy << (tpl_end ? tpl[0,tpl_end] : tpl)
21
- tpl = tpl_start ? tpl[tpl_start,tpl.length] : ''
22
- end while tpl.length > 0
23
-
24
- tar_end_editable_str = "<!-- InstanceEndEditable -->"
25
- tar = File.open(opts[:file]).read
26
- tar_copy = []
27
-
28
- begin
29
- tar_start = tar.index("<!-- InstanceBeginEditable")
30
- tar_end = tar.index(tar_end_editable_str)
31
- tar_end = tar_end + tar_end_editable_str.length if tar_end
32
- tar_copy << tar[tar_start,tar_end] if tar_start
33
- tar = tar_end ? tar[tar_end,tar.length] : ''
34
- end while tar.length > 0
35
-
36
- new = ''
37
- (0..(tar_copy.length)).to_a.each do |i|
38
- new = new += tpl_copy[i]
39
- new = new += tar_copy[i] if tar_copy[i]
40
- end
41
-
42
- puts new