Skip to content

Commit

Permalink
Merge pull request #47 from matze-dd/biblatex
Browse files Browse the repository at this point in the history
Biblatex
  • Loading branch information
matze-dd authored Aug 7, 2020
2 parents c2ebf0b + 43497ce commit 09a9a10
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Work in progress
----------------
- added extension modules
- packages: graphicx, hyperref, listings, tikz
- packages: biblatex, graphicx, hyperref, listings, tikz
- document classes: article, book, report, scrartcl, scrbook
- added file list-of-macros.md
- yalafi
Expand Down
16 changes: 16 additions & 0 deletions list-of-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Please note that not everything has to be declared.
[LaTeX builtins](#latex-builtins),
[amsmath](#package-amsmath),
[amsthm](#package-amsthm),
[biblatex](#package-biblatex),
[graphicx](#package-graphicx),
[hyperref](#package-hyperref),
[listings](#package-listings),
Expand Down Expand Up @@ -147,6 +148,21 @@ multiline(\*)
proof


## Package biblatex

[yalafi/packages/biblatex.py](yalafi/packages/biblatex.py),
[tests/test\_packages/test\_biblatex.py](tests/test_packages/test_biblatex.py)

**Macros**

\\cite,
\\Cite,
\\footcite,
\\footcitetext,
\\parencite,
\\Parencite


## Package graphicx

[yalafi/packages/graphicx.py](yalafi/packages/graphicx.py),
Expand Down
39 changes: 39 additions & 0 deletions tests/test_packages/test_biblatex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@


import pytest
from yalafi import parameters, parser, utils

preamble = '\\usepackage{biblatex}\n'

def get_plain(latex):
parms = parameters.Parameters()
p = parser.Parser(parms)
plain, nums = utils.get_txt_pos(p.parse(preamble + latex))
return plain


data_test_macros_latex = [

(r'A\cite*{x}B', 'A[0]B'),
(r'A\cite[p. 15]{x}B', 'A[0, p. 15]B'),
(r'A\cite[][p. 15]{x}B', 'A[0, p. 15]B'),
(r'A\cite*[p. 15]{x}B', 'A[0, p. 15]B'),
(r'A\cite[]{x}B', 'A[0]B'),
(r'A\cite[][]{x}B', 'A[0]B'),
(r'A\cite[ ]{x}B', 'A[0, ]B'),
(r'A\cite[ ][ ]{x}B', 'A[ 0, ]B'),
(r'A\cite[See][]{x}B', 'A[See 0]B'),
(r'A\cite[See][p. 15]{x}B', 'A[See 0, p. 15]B'),
(r'A\Cite{x}B', 'A[0]B'),
(r'A\footcite{x} B', 'A B\n\n\n[0].\n'),
(r'A\footcitetext{x} B', 'A B\n\n\n[0].\n'),
(r'A\parencite{x}B', 'A[0]B'),
(r'A\Parencite{x}B', 'A[0]B'),

]

@pytest.mark.parametrize('latex,plain_expected', data_test_macros_latex)
def test_macros_latex(latex, plain_expected):
plain = get_plain(latex)
assert plain == plain_expected

1 change: 1 addition & 0 deletions yalafi/packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'*': [
'amsmath',
'amsthm',
'biblatex',
'graphicx',
'hyperref',
'listings',
Expand Down
74 changes: 74 additions & 0 deletions yalafi/packages/biblatex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

#
# YaLafi module for LaTeX package biblatex
#
# very simple approximation
# - citation text is fixed --> variable cite_text
# - we always add [] brackets (even for \cite and \footcite)
#

from yalafi import defs
from yalafi.defs import Macro, ModParm

require_packages = []

cite_text = '0'

def modify_parameters(parms):

macros_latex = ''

macros_python = [

Macro(parms, '\\cite', args='*OOA', repl=h_cite),
Macro(parms, '\\Cite', args='*OOA', repl=h_cite),
Macro(parms, '\\footcite', args='*OOA', repl=h_footcite),
Macro(parms, '\\footcitetext', args='*OOA', repl=h_footcite),
Macro(parms, '\\parencite', args='*OOA', repl=h_cite),
Macro(parms, '\\Parencite', args='*OOA', repl=h_cite),

]

environments = []

return ModParm(macros_latex=macros_latex, macros_python=macros_python,
environments=environments)


def h_cite(parser, buf, mac, args, pos):
opt1 = args[1]
opt2 = args[2]
if len(opt1) == 1 and type(opt1[0]) is defs.VoidToken:
# only [] given
opt1 = []
if opt2:
pre = opt1
if len(opt2) == 1 and type(opt2[0]) is defs.VoidToken:
# only [] given
opt2 = []
post = opt2
else:
pre = []
post = opt1

out = [defs.TextToken(pos, '[', pos_fix=True)]
if pre:
out += pre
out.append(defs.SpaceToken(out[-1].pos, ' ', pos_fix=True))
out.append(defs.TextToken(out[-1].pos, cite_text, pos_fix=True))
if post:
out += [defs.TextToken(out[-1].pos, ',', pos_fix=True),
defs.SpaceToken(out[-1].pos, ' ', pos_fix=True)]
out += post
out += [defs.TextToken(out[-1].pos, ']', pos_fix=True),
defs.ActionToken(out[-1].pos)]
return out

def h_footcite(parser, buf, mac, args, pos):
out = [defs.MacroToken(pos, '\\footnote'),
defs.SpecialToken(pos, '{')]
out += h_cite(parser, buf, mac, args, pos)
out += [defs.TextToken(out[-1].pos, '.', pos_fix=True),
defs.SpecialToken(out[-1].pos, '}'), defs.ActionToken(out[-1].pos)]
return out

0 comments on commit 09a9a10

Please sign in to comment.