2
0

Docs.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package enum Docs {
  17. package static func suffix(_ header: String, withDocs footer: String) -> String {
  18. if footer.isEmpty {
  19. return header
  20. } else {
  21. let docs = """
  22. ///
  23. \(Self.inlineDocsAsNote(footer))
  24. """
  25. return header + "\n" + docs
  26. }
  27. }
  28. package static func interposeDocs(
  29. _ docs: String,
  30. between header: String,
  31. and footer: String
  32. ) -> String {
  33. let middle: String
  34. if docs.isEmpty {
  35. middle = """
  36. ///
  37. """
  38. } else {
  39. middle = """
  40. ///
  41. \(Self.inlineDocsAsNote(docs))
  42. ///
  43. """
  44. }
  45. return header + "\n" + middle + "\n" + footer
  46. }
  47. private static func inlineDocsAsNote(_ docs: String) -> String {
  48. let header = """
  49. /// > Source IDL Documentation:
  50. /// >
  51. """
  52. let body = docs.split(separator: "\n").map { line in
  53. var line = "/// > " + line.dropFirst(4)
  54. line.trimPrefix(while: { $0.isWhitespace })
  55. return String(line.drop(while: { $0.isWhitespace }))
  56. }.joined(separator: "\n")
  57. return header + "\n" + body
  58. }
  59. }