Parent: [87216c] (diff)

Child: [4f0c17] (diff)

Download this file

rcldjvu    130 lines (116 with data), 3.1 kB

#!/bin/sh
# @(#$Id: rcldjvu,v 1.2 2006-04-04 16:03:28 dockes Exp $  (C) 2005 J.F.Dockes

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#================================================================
# rcldjvu
# Extract text from a djvu file by executing djvused and djvutxt
#
# We use djvused to extract a possible title, djvutxt for the text
#
# Of course this only means anything if the djvu document actually has
# a text layer !
#
#================================================================

LANG=C ; export LANG
LC_ALL=C ; export LC_ALL
progname="rcldjvu"

# show help message
if test $# -ne 1 -o "$1" = "--help" 
then
  printf 'Convert a djvu file to HTML text for recoll indexation.\n'
  printf 'Usage: %s [infile]\n' "$progname"
  exit 1
fi

infile="$1"

iscmd()
{
    cmd=$1
    case $cmd in
    */*)
	if test -x $cmd ; then return 0; else return 1; fi ;;
    *)
      oldifs=$IFS; IFS=":"; set -- $PATH; IFS=$oldifs
      for d in $*;do test -x $d/$cmd && return 0;done
      return 1 ;;
    esac
}
checkcmds()
{
    for cmd in $*;do
      if iscmd $cmd 
      then 
        a=1
      else 
        echo $cmd not found 1>&2 
	exit 1
      fi
    done
}
checkcmds djvutxt djvused awk

# check the input file existence
if test ! -f "$infile"
then
  printf '%s: %s: no such file\n' "$progname" "$infile"
  exit 1
fi

# Title: we try to extract it from the annotations. djvused outputs string
# in C/awk \-escaped notation. Awk can only process this in string
# constants, so we have a first awk pass to create an awk program to parse
# the string as a constant (...). This is not exactly robust or nice
title=`djvused "$infile" -e 'select 1;output-ant' | \
grep ' (title ' | sed -e 's/^.* (title //' -e 's/)$//' |\
awk '
{
      printf("BEGIN {s = %s; print s}\n", $0)
}' | awk -f -`


cat <<EOF
<html>
<head>
    <title>$title</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<pre>
EOF

djvutxt "$infile" | sed -e 's/[ 	][ 	]*$//' | \
awk '
BEGIN {
  cont = ""
}
{
    $0 = cont $0
    cont = ""

    if ($0 == "\f") {
       print "</p>\n<hr>\n<p>"
       next
    } else if ($0 ~ /[-]$/) {
      # Break at last whitespace
      match($0, "[ \t][^ \t]+$")
      line = substr($0, 0, RSTART)
      cont = substr($0, RSTART, RLENGTH)
      $0 = line
      gsub("-", "", cont)
    }
    gsub(/&/, "\\&amp;", $0)
    gsub(/</, "\\&lt;", $0)
    gsub(/>/, "\\&gt;", $0)
    print $0      
}'

cat <<EOF
</pre>
</body>
</html>
EOF