Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

75 lignes
1.7 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package gosrc
  7. import (
  8. "path/filepath"
  9. "regexp"
  10. "time"
  11. )
  12. type Presentation struct {
  13. Filename string
  14. Files map[string][]byte
  15. Updated time.Time
  16. }
  17. type presBuilder struct {
  18. filename string
  19. data []byte
  20. resolveURL func(fname string) string
  21. fetch func(fnames []string) ([]*File, error)
  22. }
  23. var assetPat = regexp.MustCompile(`(?m)^\.(play|code|image|iframe|html)\s+(?:-\S+\s+)*(\S+)`)
  24. func (b *presBuilder) build() (*Presentation, error) {
  25. var data []byte
  26. var fnames []string
  27. i := 0
  28. for _, m := range assetPat.FindAllSubmatchIndex(b.data, -1) {
  29. name := filepath.Clean(string(b.data[m[4]:m[5]]))
  30. switch string(b.data[m[2]:m[3]]) {
  31. case "iframe", "image":
  32. data = append(data, b.data[i:m[4]]...)
  33. data = append(data, b.resolveURL(name)...)
  34. case "html":
  35. // TODO: sanitize and fix relative URLs in HTML.
  36. data = append(data, "\nERROR: .html not supported\n"...)
  37. case "play", "code":
  38. data = append(data, b.data[i:m[5]]...)
  39. found := false
  40. for _, n := range fnames {
  41. if n == name {
  42. found = true
  43. break
  44. }
  45. }
  46. if !found {
  47. fnames = append(fnames, name)
  48. }
  49. default:
  50. data = append(data, "\nERROR: unknown command\n"...)
  51. }
  52. i = m[5]
  53. }
  54. data = append(data, b.data[i:]...)
  55. files, err := b.fetch(fnames)
  56. if err != nil {
  57. return nil, err
  58. }
  59. pres := &Presentation{
  60. Updated: time.Now().UTC(),
  61. Filename: b.filename,
  62. Files: map[string][]byte{b.filename: data},
  63. }
  64. for _, f := range files {
  65. pres.Files[f.Name] = f.Data
  66. }
  67. return pres, nil
  68. }