| 1 | package HiD::Generator::BibtexPage; | ||
| 2 | # ABSTRACT: HiD Bibtex publication list page generator | ||
| 3 | use Moose; | ||
| 4 | with 'HiD::Generator'; | ||
| 5 | use HiD::Page; | ||
| 6 | use Text::BibTeX; | ||
| 7 | use Text::BibTeX::Bib; | ||
| 8 | |||
| 9 | our $VERSION = "0.3"; | ||
| 10 | |||
| 11 | |||
| 12 | # a map of bibtex publication item names to real/speaking names | ||
| 13 | my $type_map = { | ||
| 14 | 'inproceedings' => 'Conference', | ||
| 15 | 'phdthesis' => 'PhD Thesis', | ||
| 16 | 'article' => 'Journal', | ||
| 17 | 'misc' => 'Misc', | ||
| 18 | 'mastersthesis' => 'Diploma Thesis', | ||
| 19 | 'unpublished' => 'Unpublished', | ||
| 20 | 'inbook' => 'Chapter in Book', | ||
| 21 | 'incollection' => 'Paper Collection' | ||
| 22 | }; | ||
| 23 | |||
| 24 | |||
| 25 | sub generate { | ||
| 26 | my( $self , $site ) = @_; | ||
| 27 | return unless $site->config->{bibtex_page}{generate}; | ||
| 28 | |||
| 29 | my $input_file = $site->config->{bibtex_page}{layout} | ||
| 30 | or die "Must define bibtex_page.layout in config if bibtex_page.generate is enabled"; | ||
| 31 | |||
| 32 | my $bibtex_file = $site->config->{bibtex_page}{bibtex} | ||
| 33 | or die "Must define bibtex_page.bibtex in config if bibtex_page.generate is enabled"; | ||
| 34 | |||
| 35 | if (! -e $site->config->{bibtex_page}{bibtex}) { | ||
| 36 | die "bibtex_page.bibtex:".$site->config->{bibtex_page}{bibtex}." file must exist\n"; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $url = $site->config->{bibtex_page}{url} // 'publications/'; | ||
| 40 | |||
| 41 | my $destination = $site->config->{bibtex_page}{destination} // $site->destination; | ||
| 42 | |||
| 43 | $self->_create_destination_directory_if_needed( $destination ); | ||
| 44 | |||
| 45 | # here we need to parse the bibtex file and generate the list | ||
| 46 | my @publications; | ||
| 47 | my $bibfile = new Text::BibTeX::File($site->config->{bibtex_page}{bibtex}); | ||
| 48 | $bibfile->set_structure ('Bib',sortby => 'year'); | ||
| 49 | while (my $entry = new Text::BibTeX::Entry $bibfile) { | ||
| 50 | next unless $entry->parse_ok; | ||
| 51 | my %pub; | ||
| 52 | $pub{type}=$type_map->{$entry->type}; | ||
| 53 | |||
| 54 | my @names = $entry->names ('author'); | ||
| 55 | my @last; | ||
| 56 | for my $n (@names){ | ||
| 57 | push(@last, $n->part('last')); | ||
| 58 | } | ||
| 59 | $pub{author}=\@last; | ||
| 60 | if ($entry->type eq "mastersthesis" || $entry->type eq "phdthesis") { | ||
| 61 | $pub{title}=$entry->get('title') . ", " . $entry->get('school') . ", " . $entry->get('address'); | ||
| 62 | } elsif($entry->type eq "inproceedings") { | ||
| 63 | $pub{title}=$entry->get('title') . ", " .$entry->get('booktitle') . ", ". $entry->get('address'); | ||
| 64 | } elsif($entry->type eq "misc") { | ||
| 65 | $pub{title}=$entry->get('title'); | ||
| 66 | if (defined($entry->get('howpublished')) && length($entry->get('howpublished'))>0) { | ||
| 67 | $pub{title}.="," . $entry->get('howpublished'); | ||
| 68 | } | ||
| 69 | if (defined($entry->get('address')) && length($entry->get('address'))>0) { | ||
| 70 | $pub{title}.="," . $entry->get('address'); | ||
| 71 | } | ||
| 72 | } elsif($entry->type eq "inbook") { | ||
| 73 | $pub{title} = "Chap: " . $entry->get('chapter') . ", Book: " . $entry->get('title'); | ||
| 74 | $pub{title} .= ", Editor: " . $entry->get('editor') unless !defined($entry->get('editor')); | ||
| 75 | $pub{title} .= ", " . $entry->get('publisher') unless !defined($entry->get('publisher')); | ||
| 76 | } elsif($entry->type eq "incollection") { | ||
| 77 | $pub{title} = $entry->get('title') .", " . $entry->get('booktitle'); | ||
| 78 | $pub{title} .= " Vol. " . $entry->get('volume') unless !defined($entry->get('volume')); | ||
| 79 | $pub{title} .= ", Editor: " . $entry->get('editor') unless !defined($entry->get('editor')); | ||
| 80 | $pub{title} .= ", " . $entry->get('publisher') unless !defined($entry->get('publisher')); | ||
| 81 | } elsif($entry->type eq "article") { | ||
| 82 | $pub{title} = $entry->get('title') .", " . $entry->get('journal'); | ||
| 83 | $pub{title} .= " Issue " . $entry->get('number') unless !defined($entry->get('number')); | ||
| 84 | $pub{title} .= " Vol. " . $entry->get('volume') unless !defined($entry->get('volume')); | ||
| 85 | $pub{title} .= ", " . $entry->get('publisher') unless !defined($entry->get('publisher')); | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | $pub{title}=$entry->get('title'); | ||
| 89 | } | ||
| 90 | $pub{year}=$entry->get('year'); | ||
| 91 | $pub{month}=$entry->get('month'); | ||
| 92 | $pub{day}=$entry->get('day'); | ||
| 93 | $pub{url}=$entry->get('url'); | ||
| 94 | $pub{source}=$entry->print_s; | ||
| 95 | $pub{id}=$entry->key; | ||
| 96 | |||
| 97 | push(@publications, \%pub); | ||
| 98 | } | ||
| 99 | |||
| 100 | # sort publications by year | ||
| 101 | @publications=sort { $b->{year} <=> $a->{year} } @publications; | ||
| 102 | |||
| 103 | # create the new page | ||
| 104 | my $page = HiD::Page->new({ | ||
| 105 | dest_dir => $destination , | ||
| 106 | hid => $site , | ||
| 107 | url => $url , | ||
| 108 | input_filename => $input_file , | ||
| 109 | layouts => $site->layouts , | ||
| 110 | }); | ||
| 111 | $page->metadata->{publications} = \@publications; | ||
| 112 | |||
| 113 | $site->add_input( "Publication" => 'page' ); | ||
| 114 | $site->add_object( $page ); | ||
| 115 | |||
| 116 | $site->INFO( "* Injected Bibtex page"); | ||
| 117 | } | ||
| 118 | |||
| 119 | __PACKAGE__->meta->make_immutable; | ||
| 120 | 1; | ||
| 121 | |||
| 122 | __END__ | ||
| 123 | |||
| 124 | =pod | ||
| 125 | |||
| 126 | =encoding UTF-8 | ||
| 127 | |||
| 128 | =head1 NAME | ||
| 129 | |||
| 130 | HiD::Generator::BibtexPage - HiD Bibtex publication list page generator | ||
| 131 | |||
| 132 | =head1 VERSION | ||
| 133 | |||
| 134 | version 0.3 | ||
| 135 | |||
| 136 | =head1 DESCRIPTION | ||
| 137 | |||
| 138 | This Generator produces a list of publications from a bibtex file. | ||
| 139 | |||
| 140 | Enable it by setting the 'bibtex_page.generate' key in your config to true | ||
| 141 | and the 'bibtex_page.layout' key to the path with the layout for the archive | ||
| 142 | page. You can also set 'bibtex_page.url' to the URL where the page should be | ||
| 143 | published to, or let it default to the site-wide destination. Finally, | ||
| 144 | 'bibtex_page.destination' can be used to set a destination directory. | ||
| 145 | |||
| 146 | =head1 METHODS | ||
| 147 | |||
| 148 | =head2 generate | ||
| 149 | |||
| 150 | reads the configured bibtex file and generates an html version | ||
| 151 | |||
| 152 | =head1 NAME | ||
| 153 | |||
| 154 | HiD::Generator::BibtexPage - Bibtex publication list page generator | ||
| 155 | |||
| 156 | =head1 AUTHOR | ||
| 157 | |||
| 158 | Dominik Meyer <dmeyer@federationhq.de> | ||
| 159 | |||
| 160 | =head1 COPYRIGHT AND LICENSE | ||
| 161 | |||
| 162 | This software is Copyright (c) 2018 by Dominik Meyer. | ||
| 163 | |||
| 164 | This is free software, licensed under: | ||
| 165 | |||
| 166 | The GNU General Public License, Version 2, June 1991 | ||
| 167 | |||
| 168 | =cut |