Docs.swift 1.6 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. import Foundation
  17. package enum Docs {
  18. package static func suffix(_ header: String, withDocs footer: String) -> String {
  19. if footer.isEmpty {
  20. return header
  21. } else {
  22. let docs = """
  23. ///
  24. \(Self.inlineDocsAsNote(footer))
  25. """
  26. return header + "\n" + docs
  27. }
  28. }
  29. package static func interposeDocs(
  30. _ docs: String,
  31. between header: String,
  32. and footer: String
  33. ) -> String {
  34. let middle: String
  35. if docs.isEmpty {
  36. middle = """
  37. ///
  38. """
  39. } else {
  40. middle = """
  41. ///
  42. \(Self.inlineDocsAsNote(docs))
  43. ///
  44. """
  45. }
  46. return header + "\n" + middle + "\n" + footer
  47. }
  48. private static func inlineDocsAsNote(_ docs: String) -> String {
  49. let header = """
  50. /// > Source IDL Documentation:
  51. /// >
  52. """
  53. let body = docs.split(separator: "\n").map { line in
  54. "/// > " + line.dropFirst(4).trimmingCharacters(in: .whitespaces)
  55. }.joined(separator: "\n")
  56. return header + "\n" + body
  57. }
  58. }