Ejemplo n.º 1
0
func (s *Static) JsTag(staticFiles ...string) template.HTML {
	var r string
	if len(staticFiles) == 1 || !s.CombineJs {
		for _, staticFile := range staticFiles {
			r += `<script type="text/javascript" src="` + s.JsUrl(staticFile) + `" charset="utf-8"></script>`
		}
		return template.HTML(r)
	}

	r = s.CombineSavePath + "/" + com.Md5(strings.Join(staticFiles, "|")) + ".js"
	if s.IsCombined(r) == false || com.FileExists(s.RootPath+"/"+r) == false {
		var content string
		for _, url := range staticFiles {
			urlFile := s.RootPath + "/js/" + url
			if con, err := com.ReadFileS(urlFile); err != nil {
				fmt.Println(err)
			} else {
				s.RecordCombined("js/"+url, r)
				content += "\n/* <from: " + url + "> */\n"
				if !strings.Contains(url, `/min.`) && !strings.Contains(url, `.min.`) {
					b, err := minify.MinifyJS([]byte(con))
					if err != nil {
						fmt.Println(err)
					}
					con = string(b)
					con = regexCssCleanComment.ReplaceAllString(con, ``)
				}
				content += con
			}
			//fmt.Println(url)
		}
		com.WriteFile(s.RootPath+"/"+r, []byte(content))
		s.RecordCombines(r)
	}
	r = `<script type="text/javascript" src="` + s.StaticUrl(r) + `" charset="utf-8"></script>`
	return template.HTML(r)
}
Ejemplo n.º 2
0
func (s *Static) CssTag(staticFiles ...string) template.HTML {
	var r string
	if len(staticFiles) == 1 || !s.CombineCss {
		for _, staticFile := range staticFiles {
			r += `<link rel="stylesheet" type="text/css" href="` + s.CssUrl(staticFile) + `" charset="utf-8" />`
		}
		return template.HTML(r)
	}

	r = s.CombineSavePath + "/" + com.Md5(strings.Join(staticFiles, "|")) + ".css"
	if s.IsCombined(r) == false || com.FileExists(s.RootPath+"/"+r) == false {
		var content string
		for _, url := range staticFiles {
			urlFile := s.RootPath + "/css/" + url
			if con, err := com.ReadFileS(urlFile); err != nil {
				fmt.Println(err)
			} else {
				all := regexCssUrlAttr.FindAllStringSubmatch(con, -1)
				dir := path.Dir(s.CssUrl(url))
				for _, v := range all {
					res := dir
					val := v[1]
					for strings.HasPrefix(val, "../") {
						res = path.Dir(res)
						val = strings.TrimPrefix(val, "../")
					}
					con = strings.Replace(con, v[0], "url('"+res+"/"+strings.TrimLeft(val, "/")+"')", 1)
				}
				all = regexCssImport.FindAllStringSubmatch(con, -1)
				absDir := path.Dir(urlFile)
				for _, v := range all {
					val := v[1]
					res := dir
					absRes := absDir
					for strings.HasPrefix(val, "../") {
						res = path.Dir(res)
						absRes = path.Dir(absRes)
						val = strings.TrimPrefix(val, "../")
					}
					val = strings.TrimLeft(val, "/")
					//con = strings.Replace(con, v[0], `@import "`+res+"/"+val+`";`, 1)
					if icon, err := com.ReadFileS(absRes + "/" + val); err != nil {
						fmt.Println(err)
					} else {
						s.RecordCombined("css/"+strings.Trim(res, `/`)+"/"+val, r)
						con = strings.Replace(con, v[0], icon, 1)
					}
				}
				s.RecordCombined("css/"+url, r)
				content += "\n/* <from: " + url + "> */\n"
				con = regexCssCleanComment.ReplaceAllString(con, ``)
				con = regexCssCleanSpace.ReplaceAllString(con, `$1`)
				con = regexCssCleanSpace2.ReplaceAllString(con, ` `)
				content += con
			}
		}
		com.WriteFile(s.RootPath+"/"+r, []byte(content))
		s.RecordCombines(r)
	}
	r = `<link rel="stylesheet" type="text/css" href="` + s.StaticUrl(r) + `" charset="utf-8" />`
	return template.HTML(r)
}