View Single Post
Old 01-22-2009, 05:23 PM   #14
sehrgut
Junior Member
sehrgut began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Jan 2009
Device: prs500, prs350, wexler flexone
h2lwrap

I'm in the process of cleaning up a bunch of text ebooks, and html is the easiest and most versatile structured format to generate and store the "masters". I started looking for a util to pass meta-tags on to html2lrf, and found this. Unfortunately, as my work is all on OS X and Linux, I can't use pepak's util.

Below is something I tossed together that handles any corner cases I was able to come up with rather well. It has the added benefit of running anywhere you have bash and sed. (Namely, anywhere you _can't_ use the win32 util. *grin*)

Thanks for the idea, pepak!

Code:
#!/bin/sh

# h2lwrap harvests meta tags from file $1 in the namespace $prefix
# and runs html2lrf on $1 with those tags as options.
#
# For compatibility with pepak's h2lrf utility, it permits meta
# names in the form "$prefix:--option" as well.
#
# EXAMPLE
#
# <meta name="lrf:author" content="Anthony Burgess" />
# <meta name="lrf:author-sort" content="Burgess, Anthony" />
# <meta name="lrf:title" content="A Clockwork Orange" />
# <meta name="lrf:title-sort" content="Clockwork Orange, A" />
# <meta name="lrf:headerformat" content="%t, %a" />
# <meta name="lrf:page-break-before-tag" content="$" />
# <meta name="lrf:disable-chapter-detection" />
# <meta name="lrf:header" />
#
# todo: "@include" support
#

prefix=lrf
verstring='h2lwrap v0.1a keith beckman 012209'

function getmeta () {
	sed -Ef /dev/fd/7 7<<EOF
s,<meta +name="${prefix}:(--)?([^"]+)" +content="(.+)"( +)?/?>,--\2="\3",
t end
s,<meta +name="${prefix}:(--)?([^"]+)"( +)?/?>,--\2,
t end
d
:end
EOF
}

function echo_usage () {
		cat >&2 <<EOF
Usage: h2lwrap -[nhv] [file]
	h2lwrap finds ebook meta-information within html files
	and passes it along to html2lrf(1) when converting. See
	script comments for meta tag formatting.

	If file does not exist, stdin is read to file before converting.

	-n|--noconvert shows the arguments that would've been used
	-v|--version displays the version string
	-h|--help displays this help text
EOF
	}

while [ -n "$1" ]; do
	case "$1" in

		'-h'|'--help'|'-u'|'--usage')
			echo_usage
			exit 0
			;;
		'-v'|'--version')
			echo "$verstring" >&2
			exit 0
			;;
		'-n'|'--noconvert')
			noconvert=1
			;;
		*)
			input="$1"
	esac
	shift
done

if [ ! -f "$input" ]; then
	cat > "$input"
fi

args=`getmeta < "$input" | sed 's/ /\ /g' | paste -sd\  /dev/stdin`

if [ $noconvert ]; then
	echo "$args"
else
	html2lrf "$args" "$input"
fi
sehrgut is offline   Reply With Quote