preformatted.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 "fmt"
  15. // Blocks of ``` need to have blank lines on both sides or they don't look
  16. // right in HTML.
  17. func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error) {
  18. var out mungeLines
  19. inpreformat := false
  20. for i, mline := range mlines {
  21. if !inpreformat && mline.preformatted {
  22. if i == 0 || out[len(out)-1].data != "" {
  23. out = append(out, blankMungeLine)
  24. }
  25. // start of a preformat block
  26. inpreformat = true
  27. }
  28. out = append(out, mline)
  29. if inpreformat && !mline.preformatted {
  30. if i >= len(mlines)-2 || mlines[i+1].data != "" {
  31. out = append(out, blankMungeLine)
  32. }
  33. inpreformat = false
  34. }
  35. }
  36. return out, nil
  37. }
  38. // If the file ends on a preformatted line, there must have been an imbalance.
  39. func checkPreformatBalance(filePath string, mlines mungeLines) (mungeLines, error) {
  40. if len(mlines) > 0 && mlines[len(mlines)-1].preformatted {
  41. return mlines, fmt.Errorf("unbalanced triple backtick delimiters")
  42. }
  43. return mlines, nil
  44. }