Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / namespace.go
1 package revel
2
3 import (
4         "bytes"
5         "regexp"
6 )
7
8 // Module matching template syntax allows for modules to replace this text with the name of the module declared on import
9 // this allows the reverse router to use correct syntax
10 // Match _LOCAL_.static or  _LOCAL_|
11 var namespaceReplacement = regexp.MustCompile(`(_LOCAL_)(\.(.*?))?\\`)
12
13 // Function to replace the bytes data that may match the _LOCAL_ namespace specifier,
14 // the replacement will be the current module.Name
15 func namespaceReplace(fileBytes []byte, module *Module) []byte {
16         newBytes, lastIndex := &bytes.Buffer{}, 0
17         matches := namespaceReplacement.FindAllSubmatchIndex(fileBytes, -1)
18         for _, match := range matches {
19                 // Write up to first bytes
20                 newBytes.Write(fileBytes[lastIndex:match[0]])
21                 // skip ahead index to match[1]
22                 lastIndex = match[3]
23                 if match[4] > 0 {
24                         // This match includes the module name as imported by the module
25                         // We could transform the module name if it is different..
26                         // For now leave it the same
27                         // so _LOCAL_.static| becomes static|
28                         lastIndex++
29                 } else {
30                         // Inject the module name
31                         newBytes.Write([]byte(module.Name))
32                 }
33         }
34         // Write remainder of document
35         newBytes.Write(fileBytes[lastIndex:])
36         return newBytes.Bytes()
37 }