File: namespace.go

package info (click to toggle)
golang-github-revel-revel 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,240 kB
  • sloc: xml: 7; makefile: 7; javascript: 1
file content (37 lines) | stat: -rw-r--r-- 1,236 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package revel

import (
	"bytes"
	"regexp"
)

// Module matching template syntax allows for modules to replace this text with the name of the module declared on import
// this allows the reverse router to use correct syntax
// Match _LOCAL_.static or  _LOCAL_|
var namespaceReplacement = regexp.MustCompile(`(_LOCAL_)(\.(.*?))?\\`)

// Function to replace the bytes data that may match the _LOCAL_ namespace specifier,
// the replacement will be the current module.Name
func namespaceReplace(fileBytes []byte, module *Module) []byte {
	newBytes, lastIndex := &bytes.Buffer{}, 0
	matches := namespaceReplacement.FindAllSubmatchIndex(fileBytes, -1)
	for _, match := range matches {
		// Write up to first bytes
		newBytes.Write(fileBytes[lastIndex:match[0]])
		// skip ahead index to match[1]
		lastIndex = match[3]
		if match[4] > 0 {
			// This match includes the module name as imported by the module
			// We could transform the module name if it is different..
			// For now leave it the same
			// so _LOCAL_.static| becomes static|
			lastIndex++
		} else {
			// Inject the module name
			newBytes.Write([]byte(module.Name))
		}
	}
	// Write remainder of document
	newBytes.Write(fileBytes[lastIndex:])
	return newBytes.Bytes()
}