github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/www/html.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package www 4 5 import ( 6 "bytes" 7 "strconv" 8 9 "../json" 10 "../minify" 11 "../protocol" 12 ) 13 14 // mixHTMLToJS add given HTML file to specific part of JS file. 15 func mixHTMLToJS(js, html []byte) (mixedData []byte, err protocol.Error) { 16 html, err = minify.HTML.MinifyBytes(html) 17 if err != nil { 18 return 19 } 20 21 var loc = bytes.Index(js, []byte("HTML: (")) 22 if loc < 0 { 23 loc = bytes.Index(js, []byte("HTML = (")) 24 if loc < 0 { 25 // err = "html file can't add to JS file due to bad JS format." 26 return 27 } 28 } 29 30 var graveAccentIndex int = bytes.IndexByte(js[loc:], '`') 31 graveAccentIndex += loc + 1 32 33 mixedData = make([]byte, 0, len(js)+len(html)) 34 mixedData = append(mixedData, js[:graveAccentIndex]...) 35 mixedData = append(mixedData, html...) 36 mixedData = append(mixedData, js[graveAccentIndex:]...) 37 return 38 } 39 40 // mixHTMLTemplateToJS add given HTML template file to specific part of JS file. 41 func mixHTMLTemplateToJS(js, html []byte, tempName string) (mixedData []byte, err protocol.Error) { 42 html, err = minify.HTML.MinifyBytes(html) 43 if err != nil { 44 return 45 } 46 47 var loc = bytes.Index(js, []byte(tempName+`": (`)) 48 if loc < 0 { 49 // err = "html template file can't add to JS file due to bad JS format." 50 return 51 } 52 53 var graveAccentIndex int = bytes.IndexByte(js[loc:], '`') 54 graveAccentIndex += loc + 1 55 56 mixedData = make([]byte, 0, len(js)+len(html)) 57 mixedData = append(mixedData, js[:graveAccentIndex]...) 58 mixedData = append(mixedData, html...) 59 mixedData = append(mixedData, js[graveAccentIndex:]...) 60 return 61 } 62 63 // localizeHTMLFile make and returns number of localize file by number of language indicate in JSON localize text 64 func localizeHTMLFile(html []byte, lj localize) (mixedData map[string][]byte, err protocol.Error) { 65 mixedData = make(map[string][]byte, len(lj)) 66 for lang, text := range lj { 67 mixedData[lang], err = replaceLocalizeTextInHTML(html, text) 68 } 69 return 70 } 71 72 func replaceLocalizeTextInHTML(html []byte, text []string) (mixData []byte, err protocol.Error) { 73 mixData = make([]byte, 0, len(html)) 74 var replacerIndex int 75 var bracketIndex int 76 var textIndex uint64 77 var goErr error 78 for { 79 replacerIndex = bytes.Index(html, []byte("${LocaleText[")) 80 if replacerIndex < 0 { 81 mixData = append(mixData, html...) 82 return 83 } 84 mixData = append(mixData, html[:replacerIndex]...) 85 html = html[replacerIndex:] 86 87 bracketIndex = bytes.IndexByte(html, ']') 88 textIndex, goErr = strconv.ParseUint(string(html[13:bracketIndex]), 10, 8) 89 if goErr != nil { 90 // err = "Index numbers in desire file is not valid, double check your file for bad structures" 91 } else { 92 mixData = append(mixData, text[textIndex]...) 93 } 94 95 html = html[bracketIndex+1:] 96 } 97 } 98 99 type localize map[string][]string 100 101 func (lj *localize) jsonDecoder(data []byte) (err protocol.Error) { 102 // TODO::: convert to generated code 103 err = json.Unmarshal(data, lj) 104 return 105 }