analytics.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "strings"
  17. )
  18. const analyticsMungeTag = "GENERATED_ANALYTICS"
  19. const analyticsLinePrefix = "[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/"
  20. func updateAnalytics(fileName string, mlines mungeLines) (mungeLines, error) {
  21. var out mungeLines
  22. fileName, err := makeRepoRelative(fileName, fileName)
  23. if err != nil {
  24. return mlines, err
  25. }
  26. link := fmt.Sprintf(analyticsLinePrefix+"%s?pixel)]()", fileName)
  27. insertLines := getMungeLines(link)
  28. mlines, err = removeMacroBlock(analyticsMungeTag, mlines)
  29. if err != nil {
  30. return mlines, err
  31. }
  32. // Remove floating analytics links not surrounded by the munge tags.
  33. for _, mline := range mlines {
  34. if mline.preformatted || mline.header || mline.beginTag || mline.endTag {
  35. out = append(out, mline)
  36. continue
  37. }
  38. if strings.HasPrefix(mline.data, analyticsLinePrefix) {
  39. continue
  40. }
  41. out = append(out, mline)
  42. }
  43. out = appendMacroBlock(out, analyticsMungeTag)
  44. out, err = updateMacroBlock(out, analyticsMungeTag, insertLines)
  45. if err != nil {
  46. return mlines, err
  47. }
  48. return out, nil
  49. }