1
#!/bin/bash
2
3
# XHTML2ODT -- Minimalist bash-script to run the stylesheets
4
#
5
# Project URL: http://xhtml2odt.org
6
# Copyright (C) 2009-2010 Aurelien Bompard
7
# License: GNU LGPL version 2.1 or later:
8
#          http://www.gnu.org/licenses/lgpl-2.1.html
9
#
10
# This script has no remote or local image support, no keywords, and in general
11
# no advanced features such as those of the PHP and Python scripts. Its purpose
12
# is to demonstrate the simplest use of the stylesheets, in a language many can
13
# understand.
14
#
15
# Requires: unzip, tidy, xsltproc
16
17
INSTALL_PATH="."
18
19
if [ $# -ne 3 ]; then
20
    echo "Usage: $0 <html-input-file> <odt-output-file> <odt-template-file>"
21
    exit 2
22
fi
23
24
htmlfile="$1"
25
odtfile="$2"
26
templatefile="$3"
27
28
if [ ! -f "$htmlfile" ]; then
29
    echo "Can't find input file: $htmlfile. Aborting." >&2
30
    exit 1
31
fi
32
33
if [ ! -f "$templatefile" ]; then
34
    echo "Can't find template file: $templatefile. Aborting." >&2
35
    exit 1
36
fi
37
38
if [ ! -d "$INSTALL_PATH/xsl" ]; then
39
    echo "Can't find the stylesheets in $INSTALL_PATH/xsl. Aborting" >&2
40
    exit 1
41
fi
42
43
# Unzip the template
44
tmpdir=`mktemp -d --tmpdir xhtml2odt-XXXX`
45
trap "rm -rf $tmpdir" EXIT
46
unzip -q -d $tmpdir/odt $templatefile
47
48
# Clean up the HTML
49
tidy -quiet -asxhtml -utf8 $htmlfile > $tmpdir/tidied.html
50
if [ $? -ne 0 ]; then
51
    echo "Tidy could not clean up the XHTML, aborting." >&2
52
    exit 1
53
fi
54
55
# Convert the XHTML
56
xsltproc $INSTALL_PATH/xsl/xhtml2odt.xsl $tmpdir/tidied.html \
57
    > $tmpdir/converted.xml
58
if [ $? -ne 0 ]; then
59
    echo "Impossible to convert the XHTML to ODT, aborting." >&2
60
    exit 1
61
fi
62
sed -i -e 's/<?xml version="1.0" encoding="utf-8"?>//' $tmpdir/converted.xml
63
64
# Add it to the template
65
sed -i -e 's,</office:text>.*</office:body>.*</office:document-content>,,' $tmpdir/odt/content.xml
66
cat $tmpdir/converted.xml >> $tmpdir/odt/content.xml
67
echo "</office:text></office:body></office:document-content>" >> $tmpdir/odt/content.xml
68
69
# Add missing styles and fonts
70
xsltproc $INSTALL_PATH/xsl/styles.xsl $tmpdir/odt/content.xml \
71
    > $tmpdir/odt/content.new.xml
72
mv $tmpdir/odt/content.new.xml $tmpdir/odt/content.xml
73
xsltproc $INSTALL_PATH/xsl/styles.xsl $tmpdir/odt/styles.xml \
74
    > $tmpdir/odt/styles.new.xml
75
mv $tmpdir/odt/styles.new.xml $tmpdir/odt/styles.xml
76
77
# Rebuild the zip file
78
pushd $tmpdir/odt >/dev/null
79
zip -q -r ../output.odt .
80
popd >/dev/null
81
mv $tmpdir/output.odt $odtfile
82
83
echo "The converted document has been written to $odtfile"