github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/tpl/tplimpl/ace.go (about) 1 // Copyright 2017-present The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package tplimpl 15 16 import ( 17 "html/template" 18 "path/filepath" 19 20 "strings" 21 22 "github.com/yosssi/ace" 23 ) 24 25 func (t *templateHandler) addAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error { 26 t.checkState() 27 var base, inner *ace.File 28 withoutExt := name[:len(name)-len(filepath.Ext(innerPath))] 29 name = withoutExt + ".html" 30 31 // Fixes issue #1178 32 basePath = strings.Replace(basePath, "\\", "/", -1) 33 innerPath = strings.Replace(innerPath, "\\", "/", -1) 34 35 if basePath != "" { 36 base = ace.NewFile(basePath, baseContent) 37 inner = ace.NewFile(innerPath, innerContent) 38 } else { 39 base = ace.NewFile(innerPath, innerContent) 40 inner = ace.NewFile("", []byte{}) 41 } 42 43 parsed, err := ace.ParseSource(ace.NewSource(base, inner, []*ace.File{}), nil) 44 if err != nil { 45 t.errors = append(t.errors, &templateErr{name: name, err: err}) 46 return err 47 } 48 49 templ, err := ace.CompileResultWithTemplate(t.html.t.New(name), parsed, nil) 50 if err != nil { 51 t.errors = append(t.errors, &templateErr{name: name, err: err}) 52 return err 53 } 54 55 if err := applyTemplateTransformersToHMLTTemplate(templ); err != nil { 56 return err 57 } 58 59 if strings.Contains(name, "shortcodes") { 60 // We need to keep track of one ot the output format's shortcode template 61 // without knowing the rendering context. 62 clone := template.Must(templ.Clone()) 63 t.html.t.AddParseTree(withoutExt, clone.Tree) 64 } 65 66 return nil 67 }