outline.gawk 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #! /usr/local/bin/gawk -f
  2. # texi.outline --- produce an outline from a texinfo source file
  3. #
  4. # Copyright (C) 1998 Arnold David Robbins
  5. #
  6. # TEXI.OUTLINE is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # TEXI.OUTLINE is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # NOTE:
  19. # This program uses gensub(), which is specific to gawk.
  20. # With some work (split, substr, etc), it could be made to work
  21. # on other awks, but it's not worth the trouble for me.
  22. BEGIN \
  23. {
  24. # Levels at which different nodes can be
  25. Level["@top"] = 0
  26. Level["@appendix"] = 1
  27. Level["@chapter"] = 1
  28. Level["@majorheading"] = 1
  29. Level["@unnumbered"] = 1
  30. Level["@appendixsec"] = 2
  31. Level["@heading"] = 2
  32. Level["@section"] = 2
  33. Level["@unnumberedsec"] = 2
  34. Level["@unnumberedsubsec"] = 3
  35. Level["@appendixsubsec"] = 3
  36. Level["@subheading"] = 3
  37. Level["@subsection"] = 3
  38. Level["@appendixsubsubsec"] = 4
  39. Level["@subsubheading"] = 4
  40. Level["@subsubsection"] = 4
  41. Level["@unnumberedsubsubsec"] = 4
  42. # insure that we were called correctly
  43. if (ARGC != 2) {
  44. printf("usage: %s texinfo-file\n", ARGV[0]) > "/dev/stderr"
  45. exit 1
  46. }
  47. # init header counters
  48. app_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  49. app_h = 0
  50. l1_h = l2_h = l3_h = l4_h = 0
  51. }
  52. # skip lines we're not interested in
  53. /^[^@]/ || ! ($1 in Level) { next }
  54. Level[$1] == 1 {
  55. if ($1 !~ /^@unnumbered/ || $1 !~ /heading/)
  56. l1_h++
  57. l2_h = l3_h = l4_h = 0
  58. Ntabs = 0
  59. Number = makenumber($1)
  60. Title = maketitle($0)
  61. print_title()
  62. }
  63. Level[$1] == 2 {
  64. l2_h++
  65. l3_h = l4_h = 0
  66. Ntabs = 1
  67. Number = makenumber($1)
  68. Title = maketitle($0)
  69. print_title()
  70. }
  71. Level[$1] == 3 {
  72. l3_h++
  73. l4_h = 0
  74. Ntabs = 2
  75. Number = makenumber($1)
  76. Title = maketitle($0)
  77. print_title()
  78. }
  79. Level[$1] == 4 {
  80. l4_h++
  81. Ntabs = 3
  82. Number = makenumber($1)
  83. Title = maketitle($0)
  84. print_title()
  85. }
  86. # maketitle --- extract title
  87. function maketitle(str, text)
  88. {
  89. $1 = "" # clobber section keyword
  90. text = $0
  91. gsub(/^[ \t]*/, "", text)
  92. text = gensub(/@[a-z]+{/, "", "g", text)
  93. text = gensub(/([^@])}/, "\\1", "g", text)
  94. return text
  95. }
  96. # print_title --- print the title
  97. function print_title( i)
  98. {
  99. for (i = 1; i <= Ntabs; i++)
  100. printf "\t"
  101. printf("%s %s\n", Number, Title)
  102. }
  103. # makenumber --- construct a heading number from levels and section command
  104. function makenumber(command, result, lev1)
  105. {
  106. result = ""
  107. if (command ~ /^@appendix/) {
  108. if (Level[command] == 1)
  109. app_h++
  110. lev1 = substr(app_letters, app_h, 1)
  111. } else if (command ~ /^@unnumbered/ || command ~ /heading/) {
  112. lev1 = "(unnumbered)"
  113. } else
  114. lev1 = l1_h ""
  115. result = lev1 "."
  116. if (l2_h > 0) {
  117. result = result l2_h "."
  118. if (l3_h > 0) {
  119. result = result l3_h "."
  120. if (l4_h > 0) {
  121. result = result l4_h "."
  122. }
  123. }
  124. }
  125. return result
  126. }