1/*   bibtex_create
    2     Author: poo.
    3
    4     Copyright (C) 2018 poo
    5
    6     This program is free software: you can redistribute it and/or modify
    7     it under the terms of the GNU General Public License as published by
    8     the Free Software Foundation, either version 3 of the License, or
    9     at your option) any later version.
   10
   11     This program is distributed in the hope that it will be useful,
   12     but WITHOUT ANY WARRANTY; without even the implied warranty of
   13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14     GNU General Public License for more details.
   15
   16     You should have received a copy of the GNU General Public License
   17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18
   19     05 jun 2018
   20*/
   21
   22
   23:- module(bibtex_create, [
   24	      bibtex_create/2,
   25	      bibtex_append/2
   26	  ]).

bibtex_create: Predicates for creating a BibTeX file.

*/

 entry_save(+Stream:term, +Entry:pred)
Save a BibTeX entry/3. */
   37entry_save(Stream, entry(Type, Keyword, Fields)) :-
   38    format(Stream, '@~w{~w,\n', [Type, Keyword]),
   39    fields_save(Stream, Fields),
   40    write(Stream, '}\n').
 fields_save(+Stream:term, +LstFields:list)
Save a list of fields. */
   47fields_save(_Stream, []).
   48fields_save(Stream, [field(Name, Value)]) :- !,
   49    format(Stream, '  ~w = {~w}', [Name, Value]).
   50fields_save(Stream, [field(Name, Value)|Rest]) :-
   51    format(Stream, '  ~w = {~w},\n', [Name, Value]),
   52    fields_save(Stream, Rest).
 bibtex_create(+FilePath:term, +Entries:list)
Creates a new BibTeX file storing the given Entries.
Arguments:
FilePath- A term with the path of the BibTeX file.
Entries- A list of entry/3 entries. */
   62bibtex_create(File, Entries) :-
   63    open(File, write, Stream),
   64    maplist(entry_save(Stream), Entries),
   65    close(Stream).
 bibtex_append(+FilePath:term, +Entries:list)
Appends the given entries to the provided file.
Arguments:
FilePath- A term with the path of the BibTeX file.
Entries- A list of entry/3 entries. */
   75bibtex_append(File, Entries) :-
   76    open(File, append, Stream),
   77    maplist(entry_save(Stream), Entries),
   78    close(Stream)