findtext.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #! /usr/bin/env lua
  2. local me = arg[0]:gsub(".*[/\\](.*)$", "%1")
  3. local function err(fmt, ...)
  4. io.stderr:write(("%s: %s\n"):format(me, fmt:format(...)))
  5. os.exit(1)
  6. end
  7. local output
  8. local inputs = { }
  9. local lang
  10. local author
  11. local i = 1
  12. local function usage()
  13. print([[
  14. Usage: ]]..me..[[ [OPTIONS] FILE...
  15. Extract translatable strings from the given FILE(s).
  16. Available options:
  17. -h,--help Show this help screen and exit.
  18. -o,--output X Set output file (default: stdout).
  19. -a,--author X Set author.
  20. -l,--lang X Set language name.
  21. ]])
  22. os.exit(0)
  23. end
  24. while i <= #arg do
  25. local a = arg[i]
  26. if (a == "-h") or (a == "--help") then
  27. usage()
  28. elseif (a == "-o") or (a == "--output") then
  29. i = i + 1
  30. if i > #arg then
  31. err("missing required argument to `%s'", a)
  32. end
  33. output = arg[i]
  34. elseif (a == "-a") or (a == "--author") then
  35. i = i + 1
  36. if i > #arg then
  37. err("missing required argument to `%s'", a)
  38. end
  39. author = arg[i]
  40. elseif (a == "-l") or (a == "--lang") then
  41. i = i + 1
  42. if i > #arg then
  43. err("missing required argument to `%s'", a)
  44. end
  45. lang = arg[i]
  46. elseif a:sub(1, 1) ~= "-" then
  47. table.insert(inputs, a)
  48. else
  49. err("unrecognized option `%s'", a)
  50. end
  51. i = i + 1
  52. end
  53. if #inputs == 0 then
  54. err("no input files")
  55. end
  56. local outfile = io.stdout
  57. local function printf(fmt, ...)
  58. outfile:write(fmt:format(...))
  59. end
  60. if output then
  61. local e
  62. outfile, e = io.open(output, "w")
  63. if not outfile then
  64. err("error opening file for writing: %s", e)
  65. end
  66. end
  67. if author or lang then
  68. outfile:write("\n")
  69. end
  70. if lang then
  71. printf("# Language: %s\n", lang)
  72. end
  73. if author then
  74. printf("# Author: %s\n", author)
  75. end
  76. if author or lang then
  77. outfile:write("\n")
  78. end
  79. local escapes = {
  80. ["\n"] = "\\n",
  81. ["="] = "\\=",
  82. ["\\"] = "\\\\",
  83. }
  84. local function escape(s)
  85. return s:gsub("[\\\n=]", escapes)
  86. end
  87. local messages = { }
  88. for _, file in ipairs(inputs) do
  89. local infile, e = io.open(file, "r")
  90. if infile then
  91. for line in infile:lines() do
  92. for s in line:gmatch('S%("([^"]*)"') do
  93. table.insert(messages, s)
  94. end
  95. end
  96. infile:close()
  97. else
  98. io.stderr:write(("%s: WARNING: error opening file: %s\n"):format(me, e))
  99. end
  100. end
  101. table.sort(messages)
  102. local last_msg
  103. for _, msg in ipairs(messages) do
  104. if msg ~= last_msg then
  105. printf("%s =\n", escape(msg))
  106. end
  107. last_msg = msg
  108. end
  109. if output then
  110. outfile:close()
  111. end
  112. --[[
  113. TESTS:
  114. S("foo") S("bar")
  115. S("bar")
  116. S("foo")
  117. ]]