read.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package ace
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "path/filepath"
  6. "strings"
  7. )
  8. // Special characters
  9. const (
  10. cr = "\r"
  11. lf = "\n"
  12. crlf = "\r\n"
  13. space = " "
  14. equal = "="
  15. pipe = "|"
  16. doublePipe = pipe + pipe
  17. slash = "/"
  18. sharp = "#"
  19. dot = "."
  20. doubleDot = dot + dot
  21. colon = ":"
  22. doubleColon = colon + colon
  23. doubleQuote = `"`
  24. lt = "<"
  25. gt = ">"
  26. exclamation = "!"
  27. hyphen = "-"
  28. bracketOpen = "["
  29. bracketClose = "]"
  30. )
  31. // readFiles reads files and returns source for the parsing process.
  32. func readFiles(basePath, innerPath string, opts *Options) (*source, error) {
  33. // Read the base file.
  34. base, err := readFile(basePath, opts)
  35. if err != nil {
  36. return nil, err
  37. }
  38. // Read the inner file.
  39. inner, err := readFile(innerPath, opts)
  40. if err != nil {
  41. return nil, err
  42. }
  43. var includes []*File
  44. // Find include files from the base file.
  45. if err := findIncludes(base.data, opts, &includes, base); err != nil {
  46. return nil, err
  47. }
  48. // Find include files from the inner file.
  49. if err := findIncludes(inner.data, opts, &includes, inner); err != nil {
  50. return nil, err
  51. }
  52. return NewSource(base, inner, includes), nil
  53. }
  54. // readFile reads a file and returns a file struct.
  55. func readFile(path string, opts *Options) (*File, error) {
  56. var data []byte
  57. var err error
  58. if path != "" {
  59. name := filepath.Join(opts.BaseDir, path+dot+opts.Extension)
  60. if opts.Asset != nil {
  61. data, err = opts.Asset(name)
  62. } else {
  63. data, err = ioutil.ReadFile(name)
  64. }
  65. if err != nil {
  66. return nil, err
  67. }
  68. }
  69. return NewFile(path, data), nil
  70. }
  71. // findIncludes finds and adds include files.
  72. func findIncludes(data []byte, opts *Options, includes *[]*File, targetFile *File) error {
  73. includePaths, err := findIncludePaths(data, opts, targetFile)
  74. if err != nil {
  75. return err
  76. }
  77. for _, includePath := range includePaths {
  78. if !hasFile(*includes, includePath) {
  79. f, err := readFile(includePath, opts)
  80. if err != nil {
  81. return err
  82. }
  83. *includes = append(*includes, f)
  84. if err := findIncludes(f.data, opts, includes, f); err != nil {
  85. return err
  86. }
  87. }
  88. }
  89. return nil
  90. }
  91. // findIncludePaths finds and returns include paths.
  92. func findIncludePaths(data []byte, opts *Options, f *File) ([]string, error) {
  93. var includePaths []string
  94. for i, str := range strings.Split(formatLF(string(data)), lf) {
  95. ln := newLine(i+1, str, opts, f)
  96. if ln.isHelperMethodOf(helperMethodNameInclude) {
  97. if len(ln.tokens) < 3 {
  98. return nil, fmt.Errorf("no template name is specified [file: %s][line: %d]", ln.fileName(), ln.no)
  99. }
  100. includePaths = append(includePaths, ln.tokens[2])
  101. }
  102. }
  103. return includePaths, nil
  104. }
  105. // formatLF replaces the line feed codes with LF and returns the result.
  106. func formatLF(s string) string {
  107. return strings.Replace(strings.Replace(s, crlf, lf, -1), cr, lf, -1)
  108. }
  109. // hasFile return if files has a file which has the path specified by the parameter.
  110. func hasFile(files []*File, path string) bool {
  111. for _, f := range files {
  112. if f.path == path {
  113. return true
  114. }
  115. }
  116. return false
  117. }