Source files of fsfe.org, pdfreaders.org, freeyourandroid.org, ilovefs.org, drm.info, and test.fsfe.org. Contribute: https://fsfe.org/contribute/web/
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.

odtfill 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # ==============================================================================
  3. # Fill in placeholder fields in an ODT file
  4. # ==============================================================================
  5. # Copyright © 2017,2018 Reinhard Müller <reinhard@fsfe.org>
  6. # This work is free. You can redistribute it and/or modify it under the
  7. # terms of the Do What The Fuck You Want To Public License, Version 2,
  8. # as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
  9. # ==============================================================================
  10. # example:
  11. # odtfill template.odt out.odt repeat=2 Recipient="Fred Bloggs\nHope Road 17" Means="Credit card payment" Type="Pin" Type="T-Shirt, L"
  12. if [ -z "$3" ]; then
  13. echo "Usage: $(basename $0) INFILE.odt OUTFILE.odt PLACEHOLDER=VALUE ..."
  14. exit 0
  15. fi
  16. infile=$(readlink --canonicalize-missing "$1")
  17. outfile=$(readlink --canonicalize-missing "$2")
  18. shift
  19. shift
  20. # ------------------------------------------------------------------------------
  21. # Unpack the ODT file into a temporary directory
  22. # ------------------------------------------------------------------------------
  23. tmpdir=/tmp/odtfill.$$
  24. mkdir "${tmpdir}"
  25. cd "${tmpdir}"
  26. unzip -q "${infile}"
  27. # ------------------------------------------------------------------------------
  28. # Replace the content of the placeholders
  29. # ------------------------------------------------------------------------------
  30. while [ "$1" ]; do
  31. # Split parameter name=value into name and value parts
  32. name="${1%=*}"
  33. value="${1#*=}"
  34. if [ "${name}" == "$1" ]; then
  35. echo "WARNING: ignoring invalid parameter '$1' (not in required format PLACEHOLDER=VALUE)"
  36. shift
  37. continue
  38. fi
  39. if [ "${name}" == "repeat" ]; then
  40. replacement=""
  41. for i in $(seq ${value}); do
  42. replacement="${replacement}<table:table-row\\1</table:table-row>"
  43. done
  44. # Insert special marker after the first </table:table-row>, needed because
  45. # sed is always greedy and would replace up to the last table-row in the
  46. # whole document
  47. sed --in-place --expression="s%</table:table-row>%&HERE%" content.xml
  48. sed --in-place --expression="s%<table:table-row\\(.*\\)</table:table-row>HERE%${replacement}%" content.xml
  49. shift
  50. continue
  51. fi
  52. # Replace some special characters with what they need to be in ODT
  53. value="${value//&/&amp;}"
  54. value="${value//</&lt;}"
  55. value="${value//>/&gt;}"
  56. value="${value//\\n/<text:line-break/>}"
  57. # Escape % and & for sed
  58. value="${value//%/\\%}"
  59. value="${value//&/\\&}"
  60. # Now do the replacement
  61. sed --in-place --expression="s%<text:placeholder[^>]*>&lt;${name}&gt;</text:placeholder>%${value}%" content.xml
  62. # Next please...
  63. shift
  64. done
  65. # ------------------------------------------------------------------------------
  66. # Build final ODT file and clean up
  67. # ------------------------------------------------------------------------------
  68. zip -q "${outfile}" -r *
  69. cd ${OLDPWD}
  70. rm --recursive "${tmpdir}"