Skip to content

Commit

Permalink
Guess input encoding and load case change code earlier (#1090)
Browse files Browse the repository at this point in the history
... also addresses https://tex.stackexchange.com/q/581409/

Ping back to #1058.

Reverts bits of 3165987.
  • Loading branch information
moewew committed Feb 1, 2021
1 parent 6a089ee commit 15a62fb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
2 changes: 1 addition & 1 deletion doc/latex/biblatex/biblatex.tex
Expand Up @@ -1919,7 +1919,7 @@ \subsubsection{Sorting and Encoding Issues}

\paragraph{Specifying Encodings}
\label{bib:cav:enc:enc}
When using a non-\acr{US-ASCII} encoding in the \file{bib} file, it is important to understand what \biblatex can do for you and what may require manual intervention. The package takes care of the \latex side, \ie it ensures that the data imported from the \file{bbl} file is interpreted correctly, provided that the \opt{bibencoding} package option (or the datasource specific override for this, see \secref{use:bib:res}) is set properly. All of this is handled automatically and no further steps, apart from setting the \opt{bibencoding} option in certain cases, are required. Here are a few typical usage scenarios along with the relevant lines from the document preamble:
When using a non-\acr{US-ASCII} encoding in the \file{bib} file, it is important to understand what \biblatex can do for you and what may require manual intervention. The package takes care of the \latex side, \ie it ensures that the data imported from the \file{bbl} file is interpreted correctly, provided that the \opt{bibencoding} package option (or the datasource specific override for this, see \secref{use:bib:res}) is set properly. All of this is handled automatically and no further steps, apart from setting the \opt{bibencoding} option in certain cases (namely when the encoding of the \file{bib} file differs from the encoding of the \file{tex} file), are required provided that you set up your document encoding (\ie load \sty{inputenc} or related packages if required) \emph{before} \biblatex is loaded. Here are a few typical usage scenarios along with the relevant lines from the document preamble:

\begin{itemize}
\setlength{\itemsep}{0pt}
Expand Down
97 changes: 67 additions & 30 deletions tex/latex/biblatex/biblatex.sty
Expand Up @@ -206,6 +206,14 @@
{\global\cslet{blx@pkgloaded@polyglossia}\@empty}
{}

\@ifpackageloaded{inputenc}
{\global\cslet{blx@pkgloaded@inputenc}\@empty}
{}

\@ifpackageloaded{inputenx}
{\global\cslet{blx@pkgloaded@inputenx}\@empty}
{}

% this part is deferred to \AtEndPreamble
\def\blx@packageincompatibility@endpreambleonly{%
% people should not be abusing noerroretextools,
Expand All @@ -219,6 +227,17 @@
Please do not define '\string\blx@noerroretextools'\MessageBreak
unless you really need it}}}
{}%
\@ifpackageloaded{inputenc}
{\ifcsundef{blx@pkgloaded@inputenc}
{\blx@warning@noline{Load 'inputenc' before biblatex}}
{}}
{}%
\@ifpackageloaded{inputenx}
{\ifcsundef{blx@pkgloaded@inputenx}
{}
{\blx@warning@noline{Load 'inputenx' before biblatex}}
{}}
{}%
% polyglossia pretends to be babel, so the nested structure helps
% to make sure that babel really is babel
\@ifpackageloaded{polyglossia}
Expand Down Expand Up @@ -14409,8 +14428,8 @@
\DeclareBibliographyOption[string]{texencoding}{%
\ifstrequal{#1}{auto}
{\undef\blx@texencoding}
{\def\blx@texencoding{#1}}}
{\undef\blx@texencoding@fromoption}
{\def\blx@texencoding@fromoption{#1}}}
\DeclareBibliographyOption[string]{bibencoding}{%
\ifcsdef{blx@bibenc@#1}
Expand Down Expand Up @@ -15617,7 +15636,6 @@
% Process load-time options
\ProcessOptions*
% Switch to BibTeX support if requested
\iftoggle{blx@bibtex}
{\blx@inputonce{blx-bibtex.def}{BibTeX backend compatibility}{}{}{}{}}
Expand All @@ -15640,6 +15658,41 @@
% Process preamble options
\blx@processoptions
% first stab at guessing the file encoding
\def\blx@guesstexencoding{%
\ifdef\blx@texencoding@fromoption
{\let\blx@texencoding\blx@texencoding@fromoption
\blx@info@noline{%
Input encoding '\blx@texencoding' specified}}
{\ifundef\inputencodingname
{\ifundef\XeTeXrevision
{\ifundef\luatexversion
{\def\blx@texencoding{ascii}%
\blx@info@noline{%
No input encoding detected.\MessageBreak
Assuming '\blx@texencoding'}}
{\def\blx@texencoding{utf8}%
\blx@info@noline{%
LuaTeX detected.\MessageBreak
Assuming input encoding '\blx@texencoding'}}}
{\def\blx@texencoding{utf8}%
\blx@info@noline{%
XeTeX detected.\MessageBreak
Assuming input encoding '\blx@texencoding'}}}
{\ifcsdef{blx@inpenc@\inputencodingname}
{\letcs\blx@texencoding{blx@inpenc@\inputencodingname}}
{\let\blx@texencoding\inputencodingname}%
\blx@info@noline{%
Input encoding '\blx@texencoding' detected}}}}
\blx@guesstexencoding
\let\blx@texencoding@firstguess\blx@texencoding
% this ultimately loads either
% blx-case-expl3.def or blx-case-latex2e.def
\blx@deferoption@casechanger
\blx@casechange@postload
% Deferred setup at end of preamble
\AtEndPreamble{%
\blx@checkoptions@global
Expand Down Expand Up @@ -15720,30 +15773,18 @@
\abx@dodates
\do{labeldate}}
\def\blx@checkencoding{%
\ifdef\blx@texencoding
{\blx@info@noline{%
Input encoding '\blx@texencoding' specified}}
{\ifundef\inputencodingname
{\ifundef\XeTeXrevision
{\ifundef\luatexversion
{\def\blx@texencoding{ascii}%
\blx@info@noline{%
No input encoding detected.\MessageBreak
Assuming '\blx@texencoding'}}
{\def\blx@texencoding{utf8}%
\blx@info@noline{%
LuaTeX detected.\MessageBreak
Assuming input encoding '\blx@texencoding'}}}
{\def\blx@texencoding{utf8}%
\blx@info@noline{%
XeTeX detected.\MessageBreak
Assuming input encoding '\blx@texencoding'}}}
{\ifcsdef{blx@inpenc@\inputencodingname}
{\letcs\blx@texencoding{blx@inpenc@\inputencodingname}}
{\let\blx@texencoding\inputencodingname}%
\blx@info@noline{%
Input encoding '\blx@texencoding' detected}}}%
\blx@guesstexencoding
\ifdefstrequal\blx@texencoding@firstguess\blx@texencoding
{}
{\blx@warning@noline{%
Initial guess for file encoding (\blx@texencoding@firstguess)\MessageBreak
and new guess (\blx@texencoding) do not match.\MessageBreak
Please set up your document encoding *before*\MessageBreak
loading biblatex.\MessageBreak
The case changing code might not work as expected}}%
\ifdef\blx@bibencoding
{\ifdefstring\blx@bibencoding{ascii}
{\blx@info@noline{%
Expand Down Expand Up @@ -15782,10 +15823,6 @@
\AtBeginDocument{%
\blx@auxwrite\@mainaux{}{\string\abx@aux@refcontext{\blx@refcontext@context}}
\blx@checkencoding
% this ultimately loads either
% blx-case-expl3.def or blx-case-latex2e.def
\blx@deferoption@casechanger
\blx@casechange@postload
\if@filesw
\blx@ctrlwrite
\fi
Expand Down

0 comments on commit 15a62fb

Please sign in to comment.