Class: Psd2html::Convertor

Inherits:
Object
  • Object
show all
Defined in:
lib/psd2html/convertor.rb

Constant Summary collapse

CSS_TPL =
"\.{{classname}}\{ \n{{#styles}} {{key}}:{{value}};\n{{/styles}}  \}"
CSS_HASH_BASE =
{
  "classname" => 'root-container',
  "styles" => {
    "position" => "relative"
  }
}
HTML_TPL =
"<{{tag}} {{#attributes}} {{key}}=\"{{value}}\" {{/attributes}}>{{{content}}}</{{tag}}>"
HTML_HASH_BASE =
{
  "attributes" => {
    "class" => "root-container"
  },
  "tag" => "div",
  "content" => ""
}
@@css_dictory =
{
    }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(psNode, index, dstPath) ⇒ Convertor

Returns a new instance of Convertor.



27
28
29
30
31
32
33
# File 'lib/psd2html/convertor.rb', line 27

def initialize(psNode,index,dstPath)
 @psNode = psNode
 @index = index
 @parentConvertor = nil
 @childrenConvertors = []
   @dstPath = dstPath
end

Instance Attribute Details

#childrenConvertorsObject

Returns the value of attribute childrenConvertors.



6
7
8
# File 'lib/psd2html/convertor.rb', line 6

def childrenConvertors
  @childrenConvertors
end

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/psd2html/convertor.rb', line 6

def index
  @index
end

#parentConvertorObject

Returns the value of attribute parentConvertor.



6
7
8
# File 'lib/psd2html/convertor.rb', line 6

def parentConvertor
  @parentConvertor
end

#psNodeObject

Returns the value of attribute psNode.



6
7
8
# File 'lib/psd2html/convertor.rb', line 6

def psNode
  @psNode
end

Instance Method Details

#css_mapObject

为了处理css的同名问题,需要使用一个hash来去重



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/psd2html/convertor.rb', line 72

def css_map
   Until.log("start generate css of #{@psNode.name}...")
  return unless css_skeleton
  data = css_skeleton.clone
  data["styles"] = hash_to_array(data["styles"])
  data["convertnode"] = self
  @@css_dictory[data["classname"]] = data
  @childrenConvertors.each do |node|
    node.css_map
  end
end

#css_skeletonObject

需要被重写,用于生成css的对象hash



58
59
60
# File 'lib/psd2html/convertor.rb', line 58

def css_skeleton
  CSS_HASH_BASE
end

#curleftObject



49
50
51
52
# File 'lib/psd2html/convertor.rb', line 49

def curleft
  wrapleft = @parentConvertor.psNode.respond_to?("left") ? parentConvertor.psNode.left : 0
  "#{@psNode.left-wrapleft}px"
end

#curtopObject



53
54
55
56
# File 'lib/psd2html/convertor.rb', line 53

def curtop
  wraptop = @parentConvertor.psNode.respond_to?("top") ? parentConvertor.psNode.top : 0
  "#{@psNode.top-wraptop}px"
end

#get_css_tplObject



65
66
67
# File 'lib/psd2html/convertor.rb', line 65

def get_css_tpl
  CSS_TPL
end

#get_html_tplObject



68
69
70
# File 'lib/psd2html/convertor.rb', line 68

def get_html_tpl
  HTML_TPL
end

#guidObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/psd2html/convertor.rb', line 34

def guid
   if @psNode.name.include?("|")
    className = @psNode.name.split("|")[-2]
   else
     className = @psNode.name
   end
  guidStr = className+@index.to_s
  if /\p{Han}+/u.match(className)
    Digest::MD5.hexdigest(guidStr)
  else
    guidStr
  end
  
end

#html_skeletonObject

需要被重写,用于生成html的模板渲染对象



62
63
64
# File 'lib/psd2html/convertor.rb', line 62

def html_skeleton
  HTML_HASH_BASE
end

#render_cssObject



83
84
85
86
87
88
89
90
91
# File 'lib/psd2html/convertor.rb', line 83

def render_css
  css_map()
  cssStr = ""
  @@css_dictory.values.each do |cssData|
    cssStr += "\n" + Mustache.render(cssData['convertnode'].get_css_tpl,cssData)
  end
  @@css_dictory = {}
  cssStr = sync_css(cssStr)
end

#render_htmlObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/psd2html/convertor.rb', line 93

def render_html
   Until.log("start generate html of #{@psNode.name}...")
  return "" unless html_skeleton
  data = html_skeleton.clone
  data["attributes"] = hash_to_array(data["attributes"])
        
        @childrenConvertors.each do |node|
    data["content"] += node.render_html
  end
          
  htmlStr = Mustache.render(get_html_tpl,data)
  htmlStr = sync_html(htmlStr)
end

#sync_css(cssstr) ⇒ Object



106
107
108
# File 'lib/psd2html/convertor.rb', line 106

def sync_css(cssstr)
  cssstr
end

#sync_html(htmlstr) ⇒ Object



109
110
111
# File 'lib/psd2html/convertor.rb', line 109

def sync_html(htmlstr)
  htmlstr
end