You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 regels
2.6 KiB

  1. # Copyright 2017 Google LLC
  2. #
  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. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # snipdoc merges code snippets from Go source files into a template to
  15. # produce another go file (typically doc.go).
  16. #
  17. # Call with one or more .go files and a template file.
  18. #
  19. # awk -f snipmd.awk foo.go bar.go doc.template
  20. #
  21. # In the Go files, start a snippet with
  22. # //[ NAME
  23. # and end it with
  24. # //]
  25. #
  26. # In the template, write
  27. # [NAME]
  28. # on a line by itself to insert the snippet NAME on that line.
  29. #
  30. # The following transformations are made to the Go code:
  31. # - Trailing blank lines are removed.
  32. # - `ELLIPSIS` and `_ = ELLIPSIS` are replaced by `...`
  33. /^[ \t]*\/\/\[/ { # start snippet in Go file
  34. if (inGo()) {
  35. if ($2 == "") {
  36. die("missing snippet name")
  37. }
  38. curSnip = $2
  39. next
  40. }
  41. }
  42. /^[ \t]*\/\/]/ { # end snippet in Go file
  43. if (inGo()) {
  44. if (curSnip != "") {
  45. # Remove all trailing newlines.
  46. gsub(/[\t\n]+$/, "", snips[curSnip])
  47. curSnip = ""
  48. next
  49. } else {
  50. die("//] without corresponding //[")
  51. }
  52. }
  53. }
  54. ENDFILE {
  55. if (curSnip != "") {
  56. die("unclosed snippet: " curSnip)
  57. }
  58. }
  59. /^\[.*\]$/ { # Snippet marker in template file.
  60. if (inTemplate()) {
  61. name = substr($1, 2, length($1)-2)
  62. if (snips[name] == "") {
  63. die("no snippet named " name)
  64. }
  65. printf("%s\n", snips[name])
  66. afterSnip = 1
  67. next
  68. }
  69. }
  70. # Matches every line.
  71. {
  72. if (curSnip != "") {
  73. # If the first line in the snip has no indent, add the indent.
  74. if (snips[curSnip] == "") {
  75. if (index($0, "\t") == 1) {
  76. extraIndent = ""
  77. } else {
  78. extraIndent = "\t"
  79. }
  80. }
  81. line = $0
  82. # Replace ELLIPSIS.
  83. gsub(/_ = ELLIPSIS/, "...", line)
  84. gsub(/ELLIPSIS/, "...", line)
  85. snips[curSnip] = snips[curSnip] extraIndent line "\n"
  86. } else if (inTemplate()) {
  87. afterSnip = 0
  88. # Copy to output.
  89. print
  90. }
  91. }
  92. function inTemplate() {
  93. return match(FILENAME, /\.template$/)
  94. }
  95. function inGo() {
  96. return match(FILENAME, /\.go$/)
  97. }
  98. function die(msg) {
  99. printf("%s:%d: %s\n", FILENAME, FNR, msg) > "/dev/stderr"
  100. exit 1
  101. }