#!/bin/sh

# Converts a .bib BibTeX file into a NanoBlogger article.
# Copyright (C) 2005  Romain Lenglet
#
# 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

# This script requires bibtex2html to be installed and configured.
# It has successfully been tested with bibtex2html version 1.75.
# This script also requires iconv to be installed: it is used to convert the
# generated files into UTF-8 (only UTF-8 files are generated by this script).

# Version: 0.1

# The absolute path to the .bib file:
BIBFILE="$1"

# The absolute path to the text file that contains the list of the keys of the
# only BibTeX entries to publish, one per line (it must correspond to valid
# keys in the .bib file), the other entries in the .bib will not be published
# in the generated article:
CITEFILE="$2"

# The path to the base directory of your NanoBlogger blog: 
BLOGDIR="$3"

# A text used as a title for the generated articles:
TITLE="$4"

# The prefix of article file names:
ARTICLEPREFIX="$5"


# That function modifies the HTML code generated by bibtex2html, to make it
# conform to XHTML1.1.
function tostrictxhtml() {
	sed	-e 's/<a name=/<a id=/' \
		-e 's/<pre>/<\/p><pre>/' \
		-e 's/<hr>/<hr\/>/' \
		-e 's/^<\/p>$//' \
		-e 's/<font [^>]*>//' \
		-e 's/<\/font>//' \
		-e 's/<blockquote>/<blockquote><p>/' \
		-e 's/<\/blockquote>/<\/p><\/blockquote>/'
}


(
cd "${BLOGDIR}/articles"

# Generate temporary .html files:
bibtex2html --title "TO BE REMOVED LATER" \
	--footer "<hr/><p>Generated `date +%Y-%m-%d`.</p>" \
	--style "plain" --both --charset "ISO-8859-1" \
	--citefile "${CITEFILE}" \
	--sort-by-date --reverse-sort \
	--output "${ARTICLEPREFIX}" --suffix ".html" \
	--no-header --dl --nodoc \
	"${BIBFILE}"

# Clean the generated files and convert them into NanoBlogger UTF-8
# XHTML1.1-compatible articles:
cat "${ARTICLEPREFIX}.html" \
	| tostrictxhtml | iconv -f ISO-8859-1 -t UTF-8 \
	| sed	-e 's/<h1>.*<\/h1>/'"${TITLE}"' - index/' \
	> "${ARTICLEPREFIX}.txt"

cat "${ARTICLEPREFIX}_bib.html" \
	| tostrictxhtml | iconv -f ISO-8859-1 -t UTF-8 \
	| sed	-e 's/<p><a/<a/' -e 's/<\/a><\/p>/<\/a>/' \
		-e 's/<h1>.*<\/h1>/'"${TITLE}"' - BibTeX\n/' \
	> "${ARTICLEPREFIX}_bib.txt"

cat "${ARTICLEPREFIX}_abstracts.html" \
	| tostrictxhtml | iconv -f ISO-8859-1 -t UTF-8 \
	| sed	-e 's/\([^>]\)<p>/\1<\/p><p>/' \
		-e 's/<h1>.*<\/h1>/'"${TITLE}"' - abstracts/' \
	> "${ARTICLEPREFIX}_abstracts.txt"

# Remove temporary files:
rm	"${ARTICLEPREFIX}.html" \
	"${ARTICLEPREFIX}_bib.html" \
	"${ARTICLEPREFIX}_abstracts.html"
)


