/docs/i18n/stripUntranslatable.awk

https://github.com/ManimCommunity/manim · AWK · 141 lines · 93 code · 9 blank · 39 comment · 6 complexity · 07cc97851fbe954875b98ef294e5cb4b MD5 · raw file

  1. BEGIN {
  2. # The current state of the parser:
  3. # -1 -> haven't read the first line of the new block
  4. # 0 -> reading the pre-comment
  5. # 1 -> reading the msgid
  6. # 2 -> reading the msgstr
  7. state=-1
  8. # The comment preceding the block
  9. precomment=""
  10. # The same string, but with a space after the sharp to avoid a comment
  11. sharpedprecomment=""
  12. # The msgid section, containing the string to be translated
  13. msgidstr=""
  14. # The same string, but with a sharp before every newline (commented out)
  15. sharpedmsgidstr=""
  16. # The msgstr section, containing the destination string
  17. msgstrstr=""
  18. # The same string, but with a sharp before every newline (commented out)
  19. sharpedmsgstrstr=""
  20. # Whether the block being read should be kept
  21. # -1 -> should keep, overridable
  22. # 0 -> should not keep, overridable
  23. # 1 -> should keep, not overridable
  24. acceptable=1
  25. # The file location of where to put everything
  26. # that has been stripped out
  27. untranslatablefile="./untranslatable.po"
  28. # Wether we are still reading the licence text
  29. licencetext=1
  30. }
  31. # Detecting the end of licence
  32. $0~/^#, fuzzy$/ {licencetext=0; next; next}
  33. # If we are reading the licence, skip text and dont print it.
  34. $0~// {if (licencetext==1) {next}}
  35. # We pass on the wrong metadata
  36. $0~/"Report-Msgid-Bugs-To:/ {next}
  37. $0~/"PO-Revision-Date:/ {next}
  38. $0~/"Last-Translator:/ {next}
  39. $0~/"Language-Team:/ {next}
  40. # This pattern matches empty lines
  41. # The code flushes the data stored, and save
  42. # it only if acceptable!=1
  43. $0~/^$/ {
  44. if (state<=0){
  45. if(acceptable!=1){
  46. print precomment
  47. }else{
  48. #print "# Detected untranslatable text:"
  49. #print sharpedprecomment
  50. }
  51. precomment=""
  52. }else{
  53. if(acceptable==1){
  54. print precomment
  55. print msgidstr
  56. print msgstrstr
  57. print ""
  58. }else{
  59. #print "# Detected untranslatable text:"
  60. #print sharpedprecomment
  61. #print sharpedmsgidstr
  62. #print sharpedmsgstrstr
  63. print precomment>>untranslatablefile
  64. print msgidstr>>untranslatablefile
  65. print msgstrstr>>untranslatablefile
  66. }
  67. # Add the newline currently parsed
  68. # Re-initialisation of the variables.
  69. state=-1
  70. acceptable=-1
  71. precomment=""
  72. msgidstr=""
  73. msgstrstr=""
  74. }
  75. }
  76. # If the line is commented out
  77. $0~/^#/ {
  78. precomment=(state==-1)?$0:precomment"\n"$0
  79. sharpedprecomment=(state==-1)?"# "$0:sharpedprecomment"\n# "$0
  80. state=0
  81. }
  82. # If the line starts with "msgid"
  83. $0~/^msgid/ {
  84. state=1
  85. msgidstr=$0
  86. sharpedmsgidstr="# "$0
  87. }
  88. # If the line starts with msgstr
  89. $0~/^msgstr/ {
  90. state=2
  91. msgstrstr=$0
  92. sharpedmsgstrstr="# "$0
  93. }
  94. # If the line starts with a '"'
  95. $0~/^\"/ {
  96. if(state==1){
  97. msgidstr=msgidstr"\n"$0
  98. sharpedmsgidstr=sharpedmsgidstr"\n# "$0
  99. }else{
  100. msgstrstr=msgstrstr"\n"$0
  101. sharpedmsgstrstr=sharpedmsgstrstr"\n# "$0
  102. }
  103. }
  104. # ----------------------------------------------------------------
  105. # This code is now the part that actually selects lines to be stripped out.
  106. state==1 {
  107. if($0~/^(msgid ""|"")$/){
  108. }else if($0~/^(msgid "|")((:ref:`[a-zA-Z]*`)|(:obj:)|(manim.([a-z._\\]+)"$)|(((:(mod|class|func):`\~?\.[a-zA-Z0-9._]+)`| )+))/ ) {
  109. acceptable=(acceptable==-1)?0:acceptable
  110. }else{
  111. acceptable=1
  112. }
  113. }
  114. $0~/^msgid ":ref:`[a-zA-Z]*`"/ {
  115. acceptable=0
  116. }
  117. $0~/^msgid ":obj:/ {
  118. acceptable=0
  119. }
  120. $0~/^msgid "manim.([a-z._\\]+)"$/ {
  121. acceptable=0
  122. }
  123. $0~/^msgid "((:(mod|class|func):`~\.[a-zA-Z0-9.]+)`| )+"/ {
  124. acceptable=0
  125. }
  126. $0~/^"((:(mod|class|func):`~\.[a-zA-Z0-9.]+)`| )+"/ {
  127. acceptable=0
  128. }
  129. # When the parsing is ended, print the last missing endline
  130. END {
  131. print ""
  132. }