%% hgbbib.sty
%% Bibliography Setup
%% This file is part of the Hagenberg Thesis package for creating bachelors's and masters's theses
%% Author: Wilhelm Burger (wilbur@ieee.org)
%% GitHub: https://github.com/Digital-Media/HagenbergThesis
%% File encoding: ASCII

\ProvidesPackage{hgbbib}[2025/02/24]%%AUTO

\newcommand{\@bibstyle}{numeric-comp}	%% default bibliography style is 'numeric-comp'
\DeclareOption{apa}{\renewcommand{\@bibstyle}{apa}}
\ProcessOptions\relax

\RequirePackage{xifthen}

%\usepackage[style=numeric-comp,backend=biber,bibencoding=auto]{biblatex}
\usepackage[style=\@bibstyle,backend=biber]{biblatex}
\ExecuteBibliographyOptions{
	bibencoding=auto,
	bibwarn=true,
	sortcites=true,
	defernumbers=true,
	isbn=false,
	doi=true,
	backref=true,
	backrefstyle=three}
	
\RequirePackage{csquotes}				% recommended for biblatex

%% set up dictionary terms

\RequirePackage{hgbdict}

\hgbDictionarySet{bibtitle}{english}{References}
\hgbDictionarySet{bibtitle}{german}{Quellenverzeichnis}

\hgbDictionarySet{bibtitleLiterature}{english}{Literature}
\hgbDictionarySet{bibtitleLiterature}{german}{Literatur}

\hgbDictionarySet{bibtitleMedia}{english}{Media}
\hgbDictionarySet{bibtitleMedia}{german}{Medien}

\hgbDictionarySet{bibtitleOnline}{english}{Online sources}
\hgbDictionarySet{bibtitleOnline}{german}{Online-Quellen}

\hgbDictionarySet{bibtitleSoftware}{english}{Software}
\hgbDictionarySet{bibtitleSoftware}{german}{Software}

%% titles of reference section + 3 categories of references:

\newcommand{\@bibtitle}{\hgbDictionaryGet{bibtitle}{\hgb@MainLanguage}}
\newcommand{\@bibtitleLiterature}{\hgbDictionaryGet{bibtitleLiterature}{\hgb@MainLanguage}}
\newcommand{\@bibtitleMedia}{\hgbDictionaryGet{bibtitleMedia}{\hgb@MainLanguage}}
\newcommand{\@bibtitleOnline}{\hgbDictionaryGet{bibtitleOnline}{\hgb@MainLanguage}}
\newcommand{\@bibtitleSoftware}{\hgbDictionaryGet{bibtitleSoftware}{\hgb@MainLanguage}}

%% categories for a split bibliography (order of declaration is important!)
\DeclareBibliographyCategory{literature}
\DeclareBibliographyCategory{media}
\DeclareBibliographyCategory{software}
\DeclareBibliographyCategory{online}

%% headings for the bibliography categories
\defbibheading{literature}{%
	\phantomsection%
	\section*{\@bibtitleLiterature}%
	\addcontentsline{toc}{section}{\@bibtitleLiterature}%
}

\defbibheading{media}{%
	\pagebreak[3]%
	\phantomsection%
	\section*{\@bibtitleMedia}%
	\addcontentsline{toc}{section}{\@bibtitleMedia}%
}

\defbibheading{online}{%
	\pagebreak[3]%
	\phantomsection%
	\section*{\@bibtitleOnline}%
	\addcontentsline{toc}{section}{\@bibtitleOnline}%
}

\defbibheading{software}{%
	\pagebreak[3]%
	\phantomsection%
	\section*{\@bibtitleSoftware}%
	\addcontentsline{toc}{section}{\@bibtitleSoftware}%
}

\defbibheading{noheader}[]{}		% 'none' should be defined but isn't

%Assign a given bibliography entry to one of the defined categories.
\newcommand{\@AssignToBibCategory}[1]% argument 1: entry key 
{\ifthenelse{%
		\ifentrytype{online}\OR
		\ifentrytype{www}}
	{\addtocategory{online}{#1}}%
{\ifthenelse{%
		\ifentrytype{movie}\OR
		\ifentrytype{video}\OR
		\ifentrytype{music}\OR
		\ifentrytype{audio}\OR
		\ifentrytype{image}}
	{\addtocategory{media}{#1}}%
{\ifthenelse{%
		\ifentrytype{software}\OR
		\ifentrytype{electronic}}
	{\addtocategory{software}{#1}}%
	{\addtocategory{literature}{#1}}% else (default)
}}}

%Hook provided by biblatex.
\AtEveryCitekey{\@AssignToBibCategory{\thefield{entrykey}}}

%This is only a wrapper to \addbibresource to allow the use
%of the classic bibtex-workflow in the future.
\newcommand{\AddBibFile}[1]{
	\addbibresource{#1}%
}

% Declare a source map so the sorting in \cite commands works across the bib
% categories. % The maps need to correspond to the entry types in the bib
% categories (which unfortunately can't be used directly). The source map adds
% the presort field with values from A to D that is responsible for sorting in
% the four categories.
\DeclareSourcemap{
	\maps[datatype=bibtex]{
		\map{ % All entry types except for media, software and online
			\pernottype{movie}
			\pernottype{video}
			\pernottype{music}
			\pernottype{audio}
			\pernottype{image}
			\pernottype{software}
			\pernottype{electronic}
			\pernottype{online}
			\pernottype{www}
			\step[fieldset=presort, fieldvalue = {A}]
		}
		\map{ % media entry types
			\pertype{movie}
			\pertype{video}
			\pertype{music}
			\pertype{audio}
			\pertype{image}
			\step[fieldset=presort, fieldvalue = {B}]
		}
		\map{ % software entry types
			\pertype{software}
			\pertype{electronic}
			\step[fieldset=presort, fieldvalue = {C}]
		}
		\map{ % online entry types
			\pertype{online}
			\pertype{www}
			\step[fieldset=presort, fieldvalue = {D}]
		}
	}
}

% Defines a sorting template for the nosplit option.
% This is based on Biblatex's nty sorting template but disregards the presort
% field which is generated by the source map and used to sort the bibliography
% into the four categories. See Biblatex manual section 4.5.6.
\DeclareSortingTemplate{nosplit}{
	\sort[final]{
		\field{sortkey}
	}
	\sort{
		\field{sortname}
		\field{author}
		\field{editor}
		\field{translator}
		\field{sorttitle}
		\field{title}
	}
	\sort{
		\field{sorttitle}
		\field{title}
	}
	\sort{
		\field{sortyear}
		\field{year}
	}
}

%See http://www.jr-x.de/publikationen/latex/tipps/zeilenumbruch.html
\newenvironment{bibhyphenation}% 
	{\hyphenpenalty=2%		(default 50)
	 \tolerance=9999%			(default 200)
	 \exhyphenpenalty=2%	(default 50)
	 \linepenalty=1%			(default 10)
	 \setlength{\emergencystretch}{3em}%
	 % allow URL hyphenation at any character (definitions in biblatex.def)
	 \setcounter{biburlnumpenalty}{1}%
	 \setcounter{biburlucpenalty}{1}%
	 \setcounter{biburllcpenalty}{1}%
	 \biburlsetup}%
	{}

% Currently the only public macro:
% \MakeBibliography ... creates a reference section split subsections (default)
% \MakeBibliography[nosplit] ... creates a one-piece reference section
\newcommand{\MakeBibliography}[1][]{
	% opt. arg (#1): optional argument "nosplit" to get a 1-piece bibliography
	%\clearpage
	\ifthenelse{\equal{#1}{nosplit}}%
		{% create a single bibliography with all entries:
			\printbibheading[heading=bibintoc,title={\@bibtitle}]
			\begin{bibhyphenation}	
				\begin{refcontext}[sorting=nosplit]
					\printbibliography[heading=noheader]
				\end{refcontext}
			\end{bibhyphenation}
		}%
		{% split the bibliography using the predefined categories:
			\printbibheading[heading=bibintoc,title={\@bibtitle}]
			\begin{bibhyphenation}
				\bibbycategory
			\end{bibhyphenation}
		}%
}

% Suppress unusual "In:" within journal article references. 
% see http://tex.stackexchange.com/questions/10682/suppress-in-biblatex
\renewbibmacro{in:}{%
	\ifentrytype{article}{}{%
	\printtext{\bibstring{in}\intitlepunct}}}


% To suppress warnings related to missing biblatex "drivers":
\DeclareBibliographyAlias{video}{misc}
\DeclareBibliographyAlias{movie}{misc}
\DeclareBibliographyAlias{audio}{misc}
\DeclareBibliographyAlias{software}{misc}
\DeclareBibliographyAlias{electronic}{misc}
\DeclareBibliographyAlias{image}{misc}
\DeclareBibliographyAlias{standard}{misc}
\DeclareBibliographyAlias{legislation}{misc}


% Remove the contents of the url field if a doi field is present
% To avoid duplicate DOI URLs
\AtEveryBibitem{
    \iffieldundef{doi}{}{\clearfield{url}}
}


% Use a semicolon between multiple entries in \cites
%\renewcommand*{\multicitedelim}{\addsemicolon\space}		%% abandoned, affects regular \cite{a,b,c,..} entries too

% New command for multiple citations with supplementary texts
% usage: \mcite[text1]{key1}[text2]{key2}...[textN]{keyN}
% see https://tex.stackexchange.com/a/132981
\DeclareMultiCiteCommand{\mcite}[\mkbibbrackets]{\cite}{\addsemicolon\space}


% Macro to produce cites with no backref entries
% NOTE: \citenobr is DEPRECATED, use ... {\backtrackerfalse\cite{...}} instead!
\ifthenelse{\equal{\@bibstyle}{apa}}
{
	\DeclareCiteCommand{\citenobr}% version for APA
		{\PackageWarning{hgb}{Macro \protect\citenobr\space is deprecated, use \protect\backtrackerfalse\space instead!}
		 \backtrackerfalse%
		 \usebibmacro{prenote}}
		{\usebibmacro{citeindex}%
		 \usebibmacro{cite}}%
		{\multicitedelim}
		{\usebibmacro{postnote}}
}
{
	\DeclareCiteCommand{\citenobr}%	version for numeric-comp et al.
		{\PackageWarning{hgb}{Macro \protect\citenobr\space is deprecated, use \protect\backtrackerfalse\space instead!}
		 \usebibmacro{cite:init}%
		 \bibopenbracket%
		 \backtrackerfalse%
		 \usebibmacro{prenote}%
		}
		{\usebibmacro{citeindex}%
		 \usebibmacro{cite:comp}}
		{}
		{\usebibmacro{cite:dump}%
		 \usebibmacro{postnote}%
		 \bibclosebracket}
}
	
	
	