# author: Janis Schöenefeld
# This Programm generates html bibliograpics from bibtext file.
#
# Commandline Parameters:
# bibfile :: Example: bibfile:=/home/schoenef/kogs/BIB/kogs.bib The .bib file used as database 

# Example : perl get-kogsbib-as-html-include.pl bibfile:=/home/schoenef/kogs/BIB/kogs.bib sortorder:=by_date,by_name search:=author="bernd Neumann",title="ki",year=2000

# some libraries/modules

use Text::BibTeX;
use Text::BibTeX::Bib;
use Text::BibTeX::NameFormat;
# Provides an object-oriented interface to values and simple values,
# high-level constructs that can be used to represent the strings
# associated with each field in an entry. Normally, field values are
# returned simply as Perl strings, with macros expanded and multiple
# strings "pasted" together. If desired, you can instruct Text::BibTeX
# to return Text::BibTeX::Value objects, which give you access to the
# original form of the data.
use Text::BibTeX::Value; 
use Text::BibTeX (':nodetypes'); # import "node type" constants 


# some global vars

my %clargs;
my @entry;
my $entry;
my $bibfile;
my %search;
my $debug = 0; # switches debug output on off
my $lastyear = "";
my $lastname = "";
my $firstsortorder ="";
my $complex_bib_values = "no";



# setup translation table

my%Latin2BibTable;
my%Bib2LatinTable;

$Latin2BibTable{"À"} = "\{\\\`A\}";
$Bib2LatinTable{"\\\`A"} = "À";
$Latin2BibTable{"Á"} = "\{\\\'A\}";
$Bib2LatinTable{"\\\'A"} = "Á";
$Latin2BibTable{"Â"} = "\{\\\^A\}";
$Bib2LatinTable{"\\\^A"} = "Â";
$Latin2BibTable{"Ã"} = "\{\\\~A\}";
$Bib2LatinTable{"\\\~A"} = "Ã";
$Latin2BibTable{"Ä"} = "\{\\\"A\}";
$Bib2LatinTable{"\\\"A"} = "Ä";
$Latin2BibTable{"Å"} = "\{\\AA\}";
$Bib2LatinTable{"\\AA"} = "Å";
$Latin2BibTable{"Ç"} = "\{\\c\ C\}";
$Bib2LatinTable{"\\c\ C"} = "Ç";
$Latin2BibTable{"È"} = "\{\\\`E\}";
$Bib2LatinTable{"\\\`E"} = "È";
$Latin2BibTable{"É"} = "\{\\\'E\}";
$Bib2LatinTable{"\\\'E"} = "É";
$Latin2BibTable{"Ê"} = "\{\\\^E\}";
$Bib2LatinTable{"\\\^E"} = "Ê";
$Latin2BibTable{"Ë"} = "\{\\\"E\}";
$Bib2LatinTable{"\\\"E"} = "Ë";
$Latin2BibTable{"Ì"} = "\{\\\`I\}";
$Bib2LatinTable{"\\\`I"} = "Ì";
$Latin2BibTable{"Í"} = "\{\\\'I\}";
$Bib2LatinTable{"\\\'I"} = "Í";
$Latin2BibTable{"Î"} = "\{\\\^I\}";
$Bib2LatinTable{"\\\^I"} = "Î";
$Latin2BibTable{"Ï"} = "\{\\\"I\}";
$Bib2LatinTable{"\\\"I"} = "Ï";
$Latin2BibTable{"Ñ"} = "\{\\\~N\}";
$Bib2LatinTable{"\\\~N"} = "Ñ";
$Latin2BibTable{"Ò"} = "\{\\\`O\}";
$Bib2LatinTable{"\\\`O"} = "Ò";
$Latin2BibTable{"Ó"} = "\{\\\'O\}";
$Bib2LatinTable{"\\\'O"} = "Ó";
$Latin2BibTable{"Ô"} = "\{\\\^O\}";
$Bib2LatinTable{"\\\^O"} = "Ô";
$Latin2BibTable{"Õ"} = "\{\\\~O\}";
$Bib2LatinTable{"\\\~O"} = "Õ";
$Latin2BibTable{"Ö"} = "\{\\\"O\}";
$Bib2LatinTable{"\\\"O"} = "Ö";
$Latin2BibTable{"Ø"} = "\{\\O\}";
$Bib2LatinTable{"\\O"} = "Ø";
$Latin2BibTable{"Ù"} = "\{\\\`U\}";
$Bib2LatinTable{"\\\`U"} = "Ù";
$Latin2BibTable{"Ú"} = "\{\\\'U\}";
$Bib2LatinTable{"\\\'U"} = "Ú";
$Latin2BibTable{"Û"} = "\{\\\^U\}";
$Bib2LatinTable{"\\\^U"} = "Û";
$Latin2BibTable{"Ü"} = "\{\\\"U\}";
$Bib2LatinTable{"\\\"U"} = "Ü";
$Latin2BibTable{"Ý"} = "\{\\\'Y\}";
$Bib2LatinTable{"\\\'Y"} = "Ý";
$Latin2BibTable{"ß"} = "\{\\\"s\}";
$Bib2LatinTable{"\{\\\"s\}"} = "ß";
$Latin2BibTable{"à"} = "\{\\\`a\}";
$Bib2LatinTable{"\\\`a"} = "à";
$Latin2BibTable{"á"} = "\{\\\'a\}";
$Bib2LatinTable{"\\\'a"} = "á";
$Latin2BibTable{"â"} = "\{\\\^a\}";
$Bib2LatinTable{"\\\^a"} = "â";
$Latin2BibTable{"ã"} = "\{\\\~a\}";
$Bib2LatinTable{"\\\~a"} = "ã";
$Latin2BibTable{"ä"} = "\{\\\"a\}";
$Bib2LatinTable{"\\\"a"} = "ä";
$Latin2BibTable{"å"} = "\{\\aa\}";
$Bib2LatinTable{"\\aa"} = "å";
$Latin2BibTable{"ç"} = "\{\\c\ c\}";
$Bib2LatinTable{"\\c\ c"} = "ç";
$Latin2BibTable{"è"} = "\{\\\`e\}";
$Bib2LatinTable{"\\\`e"} = "è";
$Latin2BibTable{"é"} = "\{\\\'e\}";
$Bib2LatinTable{"\\\'e"} = "é";
$Latin2BibTable{"ê"} = "\{\\\^e\}";
$Bib2LatinTable{"\\\^e"} = "ê";
$Latin2BibTable{"ë"} = "\{\\\"e\}";
$Bib2LatinTable{"\\\"e"} = "ë";
$Latin2BibTable{"ì"} = "\{\\\`i\}";
$Bib2LatinTable{"\\\`i"} = "ì";
$Latin2BibTable{"í"} = "\{\\\'i\}";
$Bib2LatinTable{"\\\'i"} = "í";
$Latin2BibTable{"î"} = "\{\\\^i\}";
$Bib2LatinTable{"\\\^i"} = "î";
$Latin2BibTable{"ï"} = "\{\\\"i\}";
$Bib2LatinTable{"\\\"i"} = "ï";
$Latin2BibTable{"ñ"} = "\{\\\~n\}";
$Bib2LatinTable{"\\\~n"} = "ñ";
$Latin2BibTable{"ò"} = "\{\\\`o\}";
$Bib2LatinTable{"\\\`o"} = "ò";
$Latin2BibTable{"ó"} = "\{\\\'o\}";
$Bib2LatinTable{"\\\'o"} = "ó";
$Latin2BibTable{"ô"} = "\{\\\^o\}";
$Bib2LatinTable{"\\\^o"} = "ô";
$Latin2BibTable{"õ"} = "\{\\\~o\}";
$Bib2LatinTable{"\\\~o"} = "õ";
$Latin2BibTable{"ö"} = "\{\\\"o\}";
$Bib2LatinTable{"\\\"o"} = "ö";
$Latin2BibTable{"ø"} = "\{\\o\}";
$Bib2LatinTable{"\\o"} = "ø";
$Latin2BibTable{"ù"} = "\{\\\`u\}";
$Bib2LatinTable{"\\\`u"} = "ù";
$Latin2BibTable{"ú"} = "\{\\\'u\}";
$Bib2LatinTable{"\\\'u"} = "ú";
$Latin2BibTable{"û"} = "\{\\\^u\}";
$Bib2LatinTable{"\\\^u"} = "û";
$Latin2BibTable{"ü"} = "\{\\\"u\}";
$Bib2LatinTable{"\\\"u"} = "ü";
$Latin2BibTable{"ý"} = "\{\\\'y\}";
$Bib2LatinTable{"\\\'y"} = "ý";
$Latin2BibTable{"ÿ"} = "\{\\\"y\}";
$Bib2LatinTable{"\\\"y"} = "ÿ";

# the Programm 
getCommandLineArguments();
#printCommandLineArguments();
readBibfile();
getSearchkrits();
sortEntrys();
searchEntrys();

sub addStructure{
    my $number = shift;
    $entry = $entry[$number];
    my $year;
    if ($entry->exists('year')){
	if($complex_bib_values eq "yes"){
	    $year = $entry->get('year')->value(0)->text;
	}else{
	    $year =  $entry->get('year');
	}
	if($firstsortorder eq "by_date"){
	    unless($year eq $lastyear){
		print "<div class='kbstructure'><b class='kbstructureyear'><a name=\"".$year."\">" .$year . "</a></b></div>\n";
		$lastyear = $year;
	    }
	}
    }
}

sub getValueType{
    if($type == BTO_MACRO){
	return "BTO_MACRO";
    }else{
	return $type;
    }
}
sub printEntryByNumber{
    my $number = shift;
    $entry = $entry[$number];
    addStructure($number);
    print "<a name=\"" . $entry->key . "\"></a>\n";
    print "<p class='kbentry'>";
    print "<span class='kb" . $entry->type() . "'>"; 
    if ($entry->type() =~ /\Abook|\Atechreport|misc|manual|journal/i){
	printSEBET1($entry);
    }elsif($entry->type() =~ /\Ainproceeding|incollection|inbook/i){
	printUEBET3($entry);
    }elsif($entry->type() =~ /\Aproceeding/i){
	printSEBET4($entry);
    }elsif($entry->type() =~ /article/i){
	printUEBET2($entry);
    }elsif($entry->type() =~ /thesis/i){
	printSEBET5($entry);
    }else{
	#print "<h3>No Format for EntryType" . $entry->type() ."</h3>";
	printSEBET1($entry);
    }
    print "<br>";
    printLinks($entry);
    print "</span>";
    print "</p>\n";
    
}

sub getCommandLineArguments{
    my $numArgs = $#ARGV + 1;
    my $i = 0;
    for($i = 0; $i < $numArgs; $i++){
	my $key = "";
	my $value = "";
	($key , $value) = split (":=" ,$ARGV[$i]);
	$clargs{$key} = $value;
    }
}

sub printCommandLineArguments(){
    my @keys = keys %clargs;
    foreach $key (@keys){
	dbout ("$key := ". $clargs{$key} ."\n");
    }
}

sub getSearchkrits(){
    if($clargs{search} eq ""){
	dbout("No Search Oders given");
	return;
    }else{
	my @searchkrits = split(",",$clargs{search});
	my $searchkrit;
	foreach $searchkrit(@searchkrits){
	    my $key = "";
	    my $value = "";
	    ($key, $value) = split("=",$searchkrit);
	    $search{$key} = $value;
	}
    }
}

sub searchEntrys{
    my @keys = keys %search;
    my $key;
    my $value;
    my $hit = 0;
    my $counter = 0;
    my $field_value;
    foreach $entry (@entry){
	$hit = 1;
	if($clargs{'asbib'} eq "true"){
	    unless($entry->key() eq $search{'entrykey'}){
		$hit = 0;
	    }
	}else{
	    foreach $key(@keys){
		next if($search{$key} eq "");
		if ($entry->exists($key)){
		    if ($complex_bib_values eq "yes"){
			$field_value = $entry->get($key)->value(0)->text;
		    }else{
			$field_value = $entry->get($key);
		    }
		    unless ($field_value =~ /$search{$key}/){
			$hit = 0;
		    }
		}else{
		    $hit = 0;
		}
	    }
	}
	if($hit == 1){
	    if($clargs{'asbib'} eq "true"){
		#print "hit: ". $entry->key();
		printEntryByNumberAsBib($counter);
	    }else{
		printEntryByNumber($counter);
	    }
	    #dbout($entry->get('year') . "  " . $entry->get('title'));
	}
	$counter++;
    }
}

sub printEntryByNumberAsBib{
    my $number = shift;
    $entry = $entry[$number];    
    my @fieldlist = $entry->fieldlist;
    my @printfields;
    foreach $field(@fieldlist){
	unless($field eq "abstract"){
	    push @printfields, "    " . "<b class =\"kbfieldname\">".$field . "</b> = \{<i class=\"kbfieldcontent\">" .$entry->get($field) ."</i>\}";
	}
    }
    print "<b class=\"kbentrytype\">\@" . $entry->type ."</b>\{<i class=\"kbentrykey\">".$entry->key()."</i>,<br>";
    print join(",<br>", @printfields). "<br>}",;
}

sub sortEntrys{
    if($clargs{sortorder} eq ""){
	dbout("No Sort Order given");
	return;
    }else{
	my @sortorder =  split(",",$clargs{sortorder});
	$firstsortorder = @sortorder[0];
	my $sortfunc;
	foreach $sortfunc(@sortorder){
	    $sortfunc =~ s/ //g;
	    @entry = sort $sortfunc @entry; 
	}
    }
}

sub readBibfile{
    my @files;
    my $file;
    unless($clargs{bibfile} eq ""){
	@files = split(",",$clargs{bibfile});
	#$file  =  $clargs{bibfile};
	my $i = 0;
	foreach $file (@files){
	    $bibfile = new Text::BibTeX::File $file;
	    if ($complex_bib_values eq "yes"){
		$bibfile->preserve_values(1);
	    }
	    if(-e $file){
		dbout("Reading " . $file);
		while ($entry = new Text::BibTeX::Entry $bibfile) {
		    next unless $entry->parse_ok;
		    $entry[$i++] = $entry;
		}
	    }else{
		print "<h3>" . $file ." not found... aborting</h3>\n";
		exit 0;
	    }
	}
    }else{
	print "<h3>No Bibfile given</h3>\n";
	exit 0;
    }
}

sub dbout{
    my $msg = shift;
    if($debug == 1){
	print "DEBUG: " . $msg;
	print "<br>\n";
    }
}

# author: Sven Utcke
sub by_date {
  my %monthno = (jan => "1",
                 feb => "2",
                 mar => "3",
                 apr => "4",
                 may => "5",
                 jun => "6",
                 jul => "7",
                 aug => "8",
                 sep => "9",
                 oct => "10",
                 nov => "11",
                 dec => "12");

  if ($a->exists('year')) {
    $a_year = $a->get('year');
  } else {
    $a_year = 0;
  }
  if ($a->exists('month')) {
    if ($monthno{$a->get('month')}) {
      $a_month = $monthno{$a->get('month')};
    } else {
      $a_month = 13;
    }
  } else {
    $a_month = 13;
  }

  if ($b->exists('year')) {
    $b_year = $b->get('year');
  } else {
    $b_year = 0;
  }
  if ($b->exists('month')) {
    if ($monthno{$b->get('month')}) {
      $b_month = $monthno{$b->get('month')};
    } else {
      $b_month = 13;
    }
  } else {
    $b_month = 13;
  }

  return +((($a_year  <=> $b_year) || 
           ($a_month <=> $b_month))*(-1));
}

sub by_name {
   # dbout("Sorting by Name");
    return ($a cmp $b);
 
}




sub nameSpan{
    my $lastname = shift;
    return "<b class='kbname' >" . $lastname . "</b>";
}

sub firstnameSpan{
    my $lastname = shift;
    return "<b class='kbfirstname' >" . $lastname . "</b>";
}

sub nameFieldToHTML{
    my $fieldcontent = shift;
    if ($complex_bib_values eq "yes"){
	if($fieldcontent == ""){ return;}
	$fieldcontent = $fieldcontent->value(0)->text;
    }
    my $type = shift;
    my $defaultpostfix = shift;
    my $csstype;
    if($type eq "editor"){
	$csstype = "kbeditor";
    }else{
	$csstype = "kbauthor";
    }
    my @names = split( " and ", $fieldcontent);
    my @formatted_names=();
    foreach $name(@names){
	if ( $name =~ /,/){
	    ($lastname,$firstname) = split (",",$name);
	    #$firstname =~ s/(\p{IsUpper})^\p{IsLower}+/$1\./g;
	#    $firstname =~ s/(\p{IsUpper})\w+/$1\./g;
	    $firstname = firstnameSpan($firstname);
	    $lastname = nameSpan($lastname);
	    $name = "<span class=". $csstype. ">".$lastname . kbdefaultSpan(", ") . $firstname."</span>". kbdefaultSpan($defaultpostfix);
	    push (@formatted_names , $name);
	}elsif($name =~ /( von | de )/i){
	    ($firstname,$lastname) = split ( $1 ,$name);
	    $lastname = $1 . $lastname;
	    $lastname = nameSpan($lastname);
	#    $firstname =~ s/(\p{IsUpper})\p{IsLower}+/$1\./g;
	    $firstname = firstnameSpan($firstname);
	    $lastname =~ s/\A\s*//g;
	    $name = "<span class=". $csstype. ">".$lastname .$firstname."</span>" . kbdefaultSpan($defaultpostfix);
	    push (@formatted_names , $name);
	}elsif(!($name =~ / / )){
	    push (@formatted_names , nameSpan($name));
	}else{
	    @nameparts = split(" ", $name);
	    $name =  nameSpan(@nameparts[(@nameparts-1)]) . ",";
	    for ( $i = 0; $i < (@nameparts-1);$i++){
		#@nameparts[$i] =~ s/(\p{IsUpper})\w+/$1\./g;
		@nameparts[$i] = firstnameSpan(@nameparts[$i]);
		$name = $name . " " . @nameparts[$i];
	    }
	    $name = "<span class=". $csstype. ">". $name ."</span>" .kbdefaultSpan($defaultpostfix);
	    push (@formatted_names, $name);	      
	}
    }
    if($csstype =~ /editor/){
	my @editors = ();
	my $counter = 0;
	my $editor;
	foreach $editor (@formatted_names){
	    unless($counter > 2){
		push @editors, $editor;
	    }
	    $counter++;
	}
	return join(kbdefaultSpan (" ; ") . "<span class='kbnameseperator'></span>" , @editors) ;
    }
    return join(kbdefaultSpan (" ; ") . "<span class='kbnameseperator'></span>" , @formatted_names) ;
}

# articles
sub printUEBET2{ 
    my $entry = shift;
    printEntryAuthors($entry); #1
    printEntryTitle($entry); #2 ,
    kbSpan("journal",$entry, "span", ". In: ");
    kbSpan("volume",$entry, "span", " ");
    #printEntryYear($entry); #9
    kbSpan("year", $entry, "span", " (",")");
    printEntryNumber($entry); #10
    printEntryPages($entry); #11
#    printLinks($entry);
}
sub printSEBET4{
   my $entry = shift;
   printEntryEditors($entry); #1
   kbSpan("organisation", $entry, "span", "","(Veranst.)");
   kbSpan("institution", $entry, "span", "","(Veranst.)");
   printEntryTitle($entry); #3
   printEntryVolume($entry); #5
#   printLinks($entry);
}

sub printSEBET5{
   my $entry = shift;
   printEntryAuthors($entry); #1
   printEntryTitle($entry); #2
   printEntryLocation($entry); #3
   printEntrySchool($entry); #4
   printEntryOrganisation($entry); #2
   printEntryInstituion($entry); #2
   printTypeofThesis($entry);
   printEntryYear($entry);
   printEntryNote($entry);
#   printLinks($entry);
}

sub printSEBET1{
    my $entry = shift;
    printEntryAuthors($entry); #1
    printEntryEditors($entry); #2
    kbSpan("institution", $entry, "span", "; ", " (Hrsg.)");
    #printEntryInstituion($entry, "(Hrsg.)");#3
    printEntryOrganisation($entry, " (Hrsg.)");#3
    #4 entfällt
    printEntryTitle($entry); #5 , 6 
    kbSpan("series", $entry, "span" , ". ");
    printEntryVolume($entry); #8
    #printNumber($entry); 9 entfällt
    printEntryBookTitle($entry); #10
    if($entry->type()  =~ /report/i){
	printTechreportType($entry);
	kbSpan("number", $entry,"span", ". ");
    }else{
	printEntryNumber($entry);
    }
    printEntryLocation($entry); #12
    printEntryPublisher($entry); #13
    printEntryYear($entry);#14
	# 15 und 16 entfällt
    printEntryNote($entry); #17
    printEntryISBN($entry); #18

#    printLinks($entry);

}

sub printUEBET3{
    my $entry = shift;
    printEntryAuthors($entry); #1
    printEntryTitle($entry); #4
#    printURL($entry);
#    printEmail($entry);
    if($entry->exists("booktitle")){
	unless($entry->get("booktitle") eq ""){
	print kbdefaultSpan(". In: ") . "<span class=\"kbin\"></span>";
    # nach SEBET1 verfahren
	my $dp = 1;
	if ($entry->exists("editor")){
	    printEntryEditors($entry," (Hrsg.)");
	    $dp = 0;
	}
	if ($entry->exists("institution")){
	    kbSpan("institution" , $entry, "span", " ; "," (Hrsg.)");
	    $df = 0;
	}
	if ($entry->exists("organisation")){
	    kbSpan( "organisation" , $entry, "span", " ; "," (Hrsg.)");
	    $dp = 0;
	}
	if($dp==1){
	    kbSpan("booktitle", $entry, "span");
	}else{
	    kbSpan("booktitle", $entry, "span", " : ");
	}
    }
    }
    kbSpan("series", $entry, "span" , ". ");
    printEntryVolume($entry); 
    printEntryLocation($entry); #12
    printEntryPublisher($entry); #13
    printEntryYear($entry);#14
	# 15 und 16 entfällt
    printEntryNote($entry); #17
    printEntryISBN($entry); #18
    printEntryPages($entry);
#    printLinks($entry);
}

sub kbSpan{
    my $field = shift;
    my $entry = shift;
    my $typeoffield = shift;
    my $defaultpraefix = shift;
    my $defaultpostfix = shift;
    my $field_value = $entry->get($field);
    my $field_type = $field;
    if($typeoffield eq ""){
	$typeoffield = "span";
    }
    my $type = $entry->type();
    if($entry->exists($field)){
	if($complex_bib_values eq "yes"){
	    $field_value = $entry->get($field)->value(0)->text;
	    $field_type = getValueType($entry->get($field)->value(0)->type);
	}
	if($field_value eq ""){
	   # print "<h3> NO FIELD TEXT IN $field</h3>";
	    return;
	}
	unless($defaultpraefix eq ""){
	    print kbdefaultSpan($defaultpraefix);
	}
	print "<". $typeoffield . " class=\"kb".$field."\">" .bibToPrint($field_value) ."</" . $typeoffield . ">";
	unless($defaultpostfix eq ""){
	    print kbdefaultSpan($defaultpostfix);
	}
    }
}
sub linkSpan{
    my $field = shift;
    my $entry = shift;
    if($entry->get($field) eq ""){return;}
    my @urls = split(",",$entry->get($field));
    my $url = @urls[0];
    print kbdefaultSpan(". URL ") ."<span class=\"kb".$field."\">".bibToPrint($url) ."</span>";
}

sub kbdefaultSpan{
    my $format = shift;
    if ($format eq ""){
	return $format;
    }
    unless($clargs{'DINSpace'}){
	$format =~ s/\A\s*([;,.:])/$1/g;
	$format =~ s/\(Hrsg\.\)/ \(Hrsg\.\)/; 
    }
    return $format;
}
sub printEmail{
    my $entry = shift;
    my $field = "email";
    if($entry->get($field) eq ""){return;}
    print "<span class=\"kbemail\">".kbdefaultSpan(". - mailto: ")."<a href=\"mailto:" . $entry->get($field) . "\">".bibToPrint($entry->get($field)) ."</a></span>";
}


sub printURL{
    my $entry = shift;
    linkSpan("url",$entry);
}


sub printEntryJournal{
    my $entry = shift;
    kbSpan("journal",$entry, "span", ". In: ");
}

sub printEntryInstituion{
    my $entry = shift;
    my $postfix = shift;
    kbSpan("institution",$entry , "span", ", ", $postfix);
}

sub printEntryOrganisation{
    my $entry = shift;
    kbSpan("organisation",$entry,"span", ", ");
}

sub printEntryYear{
    my $entry = shift;
    kbSpan("year",$entry,"",", ");
}

sub printEntryPages{
    my $entry = shift;
    kbSpan("pages",$entry,"",", S. ");
}

sub printEntryNumber{
    my $entry = shift;
    kbSpan("number",$entry, "", ", Nr. ");
}

sub printEntryISBN{
    my $entry = shift;
    kbSpan("isbn",$entry, "span", ". - ISBN ");
}

sub printEntryBookTitle{
    my $entry = shift;
    kbSpan("booktitle",$entry, "" , " : ");
}

sub printEntrySchool{
    my $entry = shift;
    kbSpan("school",$entry,"span",", ");
}

sub printTypeofThesis{
    my $entry = shift;
    my $type = "";
    if(($entry->exists('type')) && ($entry->get('type')) eq ""){
	$type = $entry->get('type');
    }elsif(($entry->exists('howpublished')) && ($entry->get('howpublished')) eq ""){
	$type = $entry->get('howpublished');
    }else{
	if($entry->type =~ /master/){
	    $type = "Diplomarbeit";
	}elsif($entry->type =~ /ph/){
	    $type = "Doktorarbeit";
	}else{
	    $type = "Hochschulschrift";
	}
    }
    print (kbdefaultSpan(", ") ."<span class=\"kbtypeofthesis\">". $type . "</span>");
}

sub printTechreportType{
    my $entry = shift;
    if($entry->type() =~ /techreport/i){
	my $type = "";
	if(($entry->exists('type')) && !($entry->get('type')) eq ""){
	    $type = $entry->get('type');
	}elsif(($entry->exists('howpublished')) && !($entry->get('howpublished')) eq ""){
	    $type = $entry->get('howpublished');
	}else{
	    $type = "Bericht";
	}
	print (kbdefaultSpan(", ") ."<span class=\"kbtypeofreport\">". $type . "</span>");
    }
}

sub printEntryNote{
    my $entry = shift;
    kbSpan("note",$entry,"span",". -  ");
}

sub printEntryLocation{
    my $entry = shift;
    kbSpan("location",$entry,"span", ". ");
}

sub printEntryVolume{
    my $entry = shift;
    kbSpan("volume",$entry, "" , ". Bd. ");
}

sub printEntryTitle{
    my $entry = shift;
    kbSpan("title", $entry,"i",": ");
}
sub printEntryAuthors{
    my $entry = shift;
    print bibToPrint(nameFieldToHTML($entry->get('author'),"author"));
}
sub printEntryEditors{
    my $entry = shift;
    my $defaultpostfix = shift;
    print bibToPrint(nameFieldToHTML($entry->get('editor'),"editor", $defaultpostfix));
}
sub printEntryPublisher{
    my $entry = shift;
    unless($entry->exists('publisher')){return;}
    my @names = $entry->names ('publisher');
    my $i=0;
    my $format = new Text::BibTeX::NameFormat ('vljf');   
    print kbdefaultSpan(" : ") . "<span class=\"kbpublisher\">";
    foreach my $name (@names) {
	if($i < 2){
	    print "<span class=\"kbpublishername\">" .bibToPrint($name->format($format)) . "</span>";
	}
	$i++;
    }
    print "</span>";
}

sub printLinks{
  my $entry = shift;
  print "<span class='kblinks'>";
  addBibTexLink($entry->key,$entry->type());
  my $pdf = 0;
  my $ps = 0;
  my $link = 0;
  my $abstract = 0;
  if ($entry->exists('html')) {
    @htmls = split /\\htmladdnormallink/ , $entry->get('html');
    foreach my $html (@htmls) {
      $html =~ s/^\s*{//;
      $html =~ s/}\s*$//;
      ($descr, $uri) = split /}\s*{/, $html;
      next unless $uri;
      $uri =~ s/}//g;
      $uri =~ s/,//g;
      print '<a class="kblink" href="' . $uri . '">[' . $descr . "]</a>  ";
    }                           # end foreach html
  } else {
    my %urls = ('url'          => "Online version",
                'abstract-url' => "Abstract");
    foreach my $url (keys %urls) {
      if ($entry->exists($url)) {
        print '<a class="kblink" href="' . $entry->get($url) . '">[' . $urls{$url} . "]</a>  ";
      }                         # end if exists
    }                           # end foreach url
  }                             # end no html
  print "</span>";
}                               # end sub printLinks

sub addBibTexLink{
  my $changelinkname=shift;
  my $doctype=shift;
  $doctype=$doctype."array";
  print << "HTML";
  <a  class="kblink"  href="http://kogs-www.informatik.uni-hamburg.de/publications/showbib?bibfiles=$clargs{bibfile}&amp;key=$changelinkname" >[BibTeX]</a> 
HTML
}                               # end sub addBibTexLink



sub latinToBibAccents{
    my $s = shift;
    my @keys = keys(%Latin2BibTable);
    my $key;
    foreach $key (@keys){
	$s =~ s/\Q$key\E/$Latin2BibTable{$key}/g;
    }
    return $s;
}


sub bibToLatinAccents{
    my $s = shift;
    my @keys = keys(%Bib2LatinTable);
    my $key;
    foreach $key (@keys){
	$s =~ s/\{\Q$key\E\}/$Bib2LatinTable{$key}/gxm;
	$s =~ s/\Q$key\E/$Bib2LatinTable{$key}/gxm;
    }
    return $s;
}



sub bibToPrint{
    my $s=shift;
    $s = bibToLatinAccents($s);
    $s=~ s/~/ /g;
    $s=~ s/\{//g;
    $s=~ s/\}//g;	    
    $s=~ s/\\//g;	    
    return $s;
}










