psd2html 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c285961d492dab7ec2462fa504d338c7cfd93801
4
- data.tar.gz: e9dbd07796a7688e57c79c3965a9e5e2ad016e7e
3
+ metadata.gz: 507d8336a81cbfb89f58e6016204d914d944da50
4
+ data.tar.gz: 4ca147dff5d8e3dffc9819c066adcaf81e6fa270
5
5
  SHA512:
6
- metadata.gz: 3d05e194a7dad02fcd2c6a529b5b29de98f4e0c03e3c7a2f750886ffaa3560d342f9a3c6e734e6dd7efb67ee0157c02eeb7cdbed69f08e0a73968bd12429f2c1
7
- data.tar.gz: 22dbd20f07b74e0628e10f1c696745bd9143a49d0b1317413bd9206a13ea7b7903b855b2582d6c0b0ee909574a9285e56e9b799e6d6d9891deb1f7069d698123
6
+ metadata.gz: 3114efbc5a00334a46b02f89f000b5f42ba0267a03b9107c7de7ab6236a11390d4f27b0520a780e3ea512435caf1c9b618a4c4f591504453cd2beeab54d73c68
7
+ data.tar.gz: 245e00d2128f24ad74035974bbb3ebaf994fcd96daae293d617e55c18cd9035a44e2a2473bde9d0848f0d1c4c5be34ee0550a927a3dc1ffda5fe4110e44c6890
@@ -1,12 +1,21 @@
1
1
  #!/usr/bin/env ruby
2
- require 'psd2html'
2
+ require_relative '../lib/psd2html.rb'
3
3
  psdPath = ARGV[0]
4
4
  dstHtmlPath = ARGV[1]
5
- psd = Psd2Html.new(psdPath)
6
- unless dstHtmlPath
7
- puts psd.render()
8
- else
5
+
6
+ if psdPath && dstHtmlPath
7
+ psd = Psd2Html.new(psdPath,dstHtmlPath)
9
8
  fh = File.new(dstHtmlPath, "w") #创建一个可写文件流
10
9
  fh.puts psd.render()
11
10
  fh.close
12
- end
11
+ else
12
+ puts <<-EOF
13
+ Please provide psdpath and htmlpath
14
+
15
+ Usage:
16
+ psd2html psdpath htmlpath
17
+ EOF
18
+ end
19
+
20
+
21
+
@@ -1,10 +1,10 @@
1
1
  require 'psd'
2
- require_relative 'psd2html/until.rb'
2
+ require_relative './until.rb'
3
3
  require_relative 'psd2html/convertor.rb'
4
4
  Dir.glob( File.expand_path( "psd2html/psconvertor/*.rb", File.dirname(__FILE__) ) ) do |convertor|
5
5
  require convertor
6
6
  end
7
- Until.log("start generate psd tree....")
7
+
8
8
  class Psd2Html
9
9
  CONVERTING_MAP = {
10
10
  "text" => ::Psd2html::PsConvertor::Text,
@@ -16,13 +16,14 @@ class Psd2Html
16
16
  "img-bg" => ::Psd2html::PsConvertor::ImgBackground,
17
17
  "root" => ::Psd2html::PsConvertor::Root
18
18
  }
19
- def initialize(psdPath)
19
+ def initialize(psdPath,dstHtmlPath)
20
+ @dstHtmlPath = dstHtmlPath
20
21
  psd = PSD.new(psdPath)
21
22
  psd.parse!
22
23
  blockRoot = psd.tree
23
24
  rootConvertor = get_convertor(blockRoot,1)
24
25
  @treeRoot = format_tree(rootConvertor)
25
- puts "start generate psd tree...."
26
+ Until.log("start generate psd tree....")
26
27
  end
27
28
 
28
29
  def render_css
@@ -48,7 +49,7 @@ class Psd2Html
48
49
  protected
49
50
  def get_convertor(node,index)
50
51
 
51
- return CONVERTING_MAP["root"].new(node,index) if node.root?
52
+ return CONVERTING_MAP["root"].new(node,index,@dstHtmlPath) if node.root?
52
53
 
53
54
  return unless node.name.include?("|")
54
55
 
@@ -56,7 +57,7 @@ class Psd2Html
56
57
  unless CONVERTING_MAP.include?(convertorName)
57
58
  return
58
59
  end
59
- CONVERTING_MAP[convertorName].new(node,index)
60
+ CONVERTING_MAP[convertorName].new(node,index,@dstHtmlPath)
60
61
  end
61
62
  def format_tree(convertor)
62
63
  if convertor.psNode.has_children?
@@ -24,11 +24,12 @@ module Psd2html
24
24
  "tag" => "div",
25
25
  "content" => ""
26
26
  }
27
- def initialize(psNode,index)
27
+ def initialize(psNode,index,dstPath)
28
28
  @psNode = psNode
29
29
  @index = index
30
30
  @parentConvertor = nil
31
31
  @childrenConvertors = []
32
+ @dstPath = dstPath
32
33
  end
33
34
  def guid
34
35
  className = @psNode.name.split("|")[-2]
@@ -40,6 +41,15 @@ module Psd2html
40
41
  end
41
42
 
42
43
  end
44
+
45
+ def curleft
46
+ wrapleft = @parentConvertor.psNode.respond_to?("left") ? parentConvertor.psNode.left : 0
47
+ "#{@psNode.left-wrapleft}px"
48
+ end
49
+ def curtop
50
+ wraptop = @parentConvertor.psNode.respond_to?("top") ? parentConvertor.psNode.top : 0
51
+ "#{@psNode.top-wraptop}px"
52
+ end
43
53
  #需要被重写,用于生成css的对象hash
44
54
  def css_skeleton
45
55
  CSS_HASH_BASE
@@ -56,6 +66,7 @@ module Psd2html
56
66
  end
57
67
  #为了处理css的同名问题,需要使用一个hash来去重
58
68
  def css_map
69
+ Until.log("start generate css of #{@psNode.name}...")
59
70
  return unless css_skeleton
60
71
  data = css_skeleton.clone
61
72
  data["styles"] = hash_to_array(data["styles"])
@@ -66,7 +77,6 @@ module Psd2html
66
77
  end
67
78
  end
68
79
  def render_css
69
- Until.log("start generate css of #{@psNode.name}...")
70
80
  css_map()
71
81
  cssStr = ""
72
82
  @@css_dictory.values.each do |cssData|
@@ -74,11 +84,10 @@ module Psd2html
74
84
  end
75
85
  @@css_dictory = {}
76
86
  cssStr = sync_css(cssStr)
77
- Until.log("done generate css of #{@psNode.name}...")
78
87
  end
79
88
 
80
89
  def render_html
81
- Until.log("start generate html of #{@psNode.name}...")
90
+ Until.log("start generate html of #{}...")
82
91
  return "" unless html_skeleton
83
92
  data = html_skeleton.clone
84
93
  data["attributes"] = hash_to_array(data["attributes"])
@@ -89,7 +98,6 @@ module Psd2html
89
98
 
90
99
  htmlStr = Mustache.render(get_html_tpl,data)
91
100
  htmlStr = sync_html(htmlStr)
92
- Until.log("done generate html of #{@psNode.name}...")
93
101
  end
94
102
  def sync_css(cssstr)
95
103
  cssstr
@@ -108,10 +116,16 @@ module Psd2html
108
116
  def css_hook(style)
109
117
  hookHash = {
110
118
  "pt" => "px",
111
- "MicrosoftYaHei" => "microsoft yahei"
119
+ "MicrosoftYaHei" => "microsoft yahei",
120
+ "rgba\\((\\s*\\d+,\\s*\\d+,\\s*\\d+),\\s*\\d+\\)" => -> { '#' + $1.split(',').map { |v| v.to_i.to_s(16) }.join }
112
121
  }
113
122
  hookHash.each do |key,value|
114
- style = style.gsub(key,value)
123
+ if value.is_a?(Proc)
124
+
125
+ style = style.gsub(Regexp.new(key)) { value.call }
126
+ else
127
+ style = style.gsub(Regexp.new(key), value)
128
+ end
115
129
  end
116
130
  style
117
131
  end
@@ -3,6 +3,7 @@ module Psd2html
3
3
  class Block < ::Psd2html::Convertor
4
4
 
5
5
  def css_skeleton
6
+
6
7
  cssRenderData = {
7
8
  "classname" => "block-#{guid}",
8
9
  "styles" => {
@@ -10,8 +11,8 @@ module Psd2html
10
11
  "display" => "inline-block",
11
12
  "width" => "#{@psNode.width}px",
12
13
  "height" => "#{@psNode.height}px",
13
- "left" => "#{@psNode.left-@parentConvertor.psNode.left}px",
14
- "top" => "#{@psNode.top-@parentConvertor.psNode.top}px",
14
+ "left" => curleft,
15
+ "top" => curtop,
15
16
  "z-index" => "#{@psNode.depth}#{@parentConvertor.childrenConvertors.length - @index.to_i}"
16
17
  }
17
18
  }
@@ -7,10 +7,11 @@ module Psd2html
7
7
  "styles" => {
8
8
  "position" => "absolute",
9
9
  "display" => "inline-block",
10
+ "border" => "none",
10
11
  "width" => "#{@psNode.width}px",
11
12
  "height" => "#{@psNode.height}px",
12
- "left" => "#{@psNode.left-@parentConvertor.psNode.left}px",
13
- "top" => "#{@psNode.top-@parentConvertor.psNode.top}px",
13
+ "left" => curleft,
14
+ "top" => curtop,
14
15
  "z-index" => "#{@psNode.depth}#{@parentConvertor.childrenConvertors.length - @index.to_i}"
15
16
  }
16
17
  }
@@ -20,8 +21,10 @@ module Psd2html
20
21
  "<{{tag}} {{#attributes}} {{key}}=\"{{value}}\" {{/attributes}} />"
21
22
  end
22
23
  def html_skeleton
23
- imgUrl = "./img-source-#{guid}-#{Time.now.to_i}.png"
24
- @psNode.image.save_as_png(imgUrl)
24
+
25
+
26
+ imgUrl = "#{File.dirname(@dstPath)}/img-source-#{guid}-#{Time.now.to_i}.png"
27
+ @psNode.image.save_as_png(imgUrl)
25
28
  htmlRenderData = {
26
29
  "attributes" => {
27
30
  "class" => "img-#{guid}",
@@ -12,22 +12,22 @@ module Psd2html
12
12
  "display" => "inline-block",
13
13
  "width" => "#{@psNode.width}px",
14
14
  "height" => "#{@psNode.height}px",
15
- "left" => "#{@psNode.left-@parentConvertor.psNode.left}px",
16
- "top" => "#{@psNode.top-@parentConvertor.psNode.top}px",
15
+ "left" => curleft,
16
+ "top" => curtop,
17
17
  "z-index" => "#{@psNode.depth}#{@parentConvertor.childrenConvertors.length - @index.to_i}"
18
18
  }
19
19
  }
20
20
  cssRenderData = CSS_HASH_BASE.merge(cssRenderData)
21
21
  end
22
22
  def html_skeleton
23
- imgUrl = "./img-source-#{guid}-#{Time.now.to_i}.png"
24
- @psNode.image.save_as_png(imgUrl)
23
+ imgUrl = "#{File.dirname(@dstPath)}/img-source-#{guid}-#{Time.now.to_i}.png"
24
+ @psNode.image.save_as_png(imgUrl)
25
25
  htmlRenderData = {
26
26
  "attributes" => {
27
27
  "class" => "img-#{guid}",
28
28
  "href" => "#"
29
29
  },
30
- "content" => "<img src=\"#{imgUrl}\" />",
30
+ "content" => "<img border=0 src=\"#{imgUrl}\" />",
31
31
  "tag" => "a"
32
32
  }
33
33
  htmlRenderData = HTML_HASH_BASE.merge(htmlRenderData)
@@ -9,8 +9,8 @@ module Psd2html
9
9
  "display" => "inline-block",
10
10
  "width" => "#{@psNode.width+2}px",
11
11
  "height" => "#{@psNode.height+2}px",
12
- "left" => "#{@psNode.left-@parentConvertor.psNode.left}px",
13
- "top" => "#{@psNode.top-@parentConvertor.psNode.top}px",
12
+ "left" => curleft,
13
+ "top" => curtop,
14
14
  "z-index" => "#{@psNode.depth}#{@parentConvertor.childrenConvertors.length - @index.to_i}"
15
15
  }
16
16
  }
@@ -1,3 +1,3 @@
1
1
  module Psd2html
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psd2html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - cherishpeace
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-13 00:00:00.000000000 Z
11
+ date: 2014-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mustache
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '0.9'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '0.9'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: psd
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -91,7 +91,7 @@ files:
91
91
  - lib/psd2html/psconvertor/textLink.rb
92
92
  - lib/psd2html/version.rb
93
93
  - lib/until.rb
94
- homepage: ''
94
+ homepage: https://github.com/cherishpeace/psd2html
95
95
  licenses:
96
96
  - MIT
97
97
  metadata: {}