class Psd2html::Convertor

Constants

CSS_HASH_BASE
CSS_TPL
HTML_HASH_BASE
HTML_TPL

Attributes

childrenConvertors[RW]
index[RW]
parentConvertor[RW]
psNode[RW]

Public Class Methods

new(psNode,index,dstPath) click to toggle source
# File lib/psd2html/convertor.rb, line 27
        def initialize(psNode,index,dstPath)
            @psNode = psNode
            @index = index
            @parentConvertor = nil
            @childrenConvertors = []
@dstPath = dstPath
    end

Public Instance Methods

css_map() click to toggle source

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

# 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_skeleton() click to toggle source

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

# File lib/psd2html/convertor.rb, line 58
def css_skeleton
   CSS_HASH_BASE
end
curleft() click to toggle source
# File lib/psd2html/convertor.rb, line 49
def curleft
  wrapleft = @parentConvertor.psNode.respond_to?("left") ? parentConvertor.psNode.left : 0
  "#{@psNode.left-wrapleft}px"
end
curtop() click to toggle source
# 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_tpl() click to toggle source
# File lib/psd2html/convertor.rb, line 65
def get_css_tpl
   CSS_TPL
end
get_html_tpl() click to toggle source
# File lib/psd2html/convertor.rb, line 68
def get_html_tpl
   HTML_TPL
end
guid() click to toggle source
# 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_skeleton() click to toggle source

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

# File lib/psd2html/convertor.rb, line 62
def html_skeleton
   HTML_HASH_BASE
end
render_css() click to toggle source
# 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_html() click to toggle source
# 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) click to toggle source
# File lib/psd2html/convertor.rb, line 106
def sync_css(cssstr)
   cssstr
end
sync_html(htmlstr) click to toggle source
# File lib/psd2html/convertor.rb, line 109
def sync_html(htmlstr)
   htmlstr
end

Protected Instance Methods

css_hook(style) click to toggle source
# File lib/psd2html/convertor.rb, line 120
  def css_hook(style)
   hookHash = {
           "pt" => "px",
           "MicrosoftYaHei" => "microsoft yahei",
"rgba\\((\\s*\\d+,\\s*\\d+,\\s*\\d+),\\s*\\d+\\)" => -> { '#' + $1.split(',').map { |v| v.to_i.to_s(16) }.join }
   }
   hookHash.each do |key,value|
if value.is_a?(Proc)
  
  style = style.gsub(Regexp.new(key)) { value.call }
else
  style = style.gsub(Regexp.new(key), value)
end
   end
   style
  end
hash_to_array(originHash) click to toggle source
# File lib/psd2html/convertor.rb, line 113
def hash_to_array(originHash)
        dstArray = []
        originHash.each do |key,value| 
                dstArray << {"key"=>key,"value"=>value}
        end
        dstArray
end