Skip to content

Commit bec2020

Browse files
committed
release v1.0
* Add `-d/--dryrun` option * LaTeX files provided using `-f/--files` option instead of positional args * Add `-v/--version` option
1 parent c07ef60 commit bec2020

File tree

6 files changed

+147
-74
lines changed

6 files changed

+147
-74
lines changed

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
# v0.1
2-
* Initial release
1+
# v1.0
2+
## Breaking changes
3+
* LaTeX source files are now provided using the `-f/--files` option rather than
4+
as positional arguments
5+
6+
## Feature additions
7+
* Add dry-run option `-d/--dryrun`
8+
* Add version option `-v/--version`
39

410
# v0.12
511
## Fixes
@@ -8,4 +14,5 @@
814
## Feature additions
915
* Add option to specify a custom install prefix
1016

11-
17+
# v0.1
18+
* Initial release

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ Assume that there are two LaTeX files: `a.tex` and `b.tex`, the reference
5656
BibTeX file is `g.bib` and we want the output file to be `refs.bib`. The
5757
command to achieve this would be:
5858
```sh
59-
bibx -b g.bib -o refs.bib a.tex b.tex
59+
bibx -b g.bib -o refs.bib -f a.tex b.tex
6060

6161
# A variation
62-
bibx -b g.bib -o refs.bib *.tex
62+
bibx -b g.bib -o refs.bib -f *.tex
6363
```
6464

6565
## Bugs
@@ -84,7 +84,7 @@ pandoc --standalone --to man bibx.1.md -o bibx.1
8484
### Tests
8585
If contributing to the code, please run the following test after your edits:
8686
```
87-
./bibx -b test/global.bib -o test/ext.bib test/*.tex
87+
./bibx -b test/global.bib -o test/ext.bib -f test/*.tex
8888
diff test/ext.bib test/ext-golden.bib
8989
```
9090
The `diff` command must return **NOTHING**. This means that the output is as

TOOD.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- [ ] Remove dependency on `ext.sh`
22
- [ ] Support multiple reference BibTeX files
33
- [ ] Support a directory of reference BibTeX files
4-
- [ ] Option to perform a dry-run (no file output, only print info to stdout)
4+
- [x] Option to perform a dry-run (no file output, only print info to stdout)

bibx

Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ import os
88
from bibtexparser.bparser import BibTexParser, BibDatabase
99
from bibtexparser.bwriter import BibTexWriter
1010

11-
def main():
11+
VERSION = "v1.0"
12+
13+
def get_parser():
1214
parser = argparse.ArgumentParser(
1315
description="""Extract bibliography entries from LaTeX sources
1416
using a reference BibTeX file""",
1517
epilog="""For reporting bugs, giving suggestions and contributing
1618
to this project, visit https://github.com/CodePurble/bibextract"""
1719
)
18-
parser.add_argument("-b",
19-
"--bib",
20-
required=True,
21-
help="BibTeX file to look for entries in"
20+
parser.add_argument("-v",
21+
"--version",
22+
help="Show version information and exit",
23+
action="store_true"
2224
)
2325
parser.add_argument("-o",
2426
"--output",
@@ -38,9 +40,10 @@ def main():
3840
help="Suppress output to stdout",
3941
action="store_true"
4042
)
41-
parser.add_argument("files",
43+
parser.add_argument("-f",
44+
"--files",
4245
help="""LaTeX files to scan for citations whose BibTeX
43-
entries need to be extract""",
46+
entries need to be extracted""",
4447
action="extend",
4548
nargs="+"
4649
)
@@ -50,56 +53,78 @@ def main():
5053
file. Default is four spaces""",
5154
default=" ",
5255
)
56+
parser.add_argument("-d",
57+
"--dryrun",
58+
help="""Perform a dry-run, i.e. do not output a file,
59+
just print info messages. Ignores the -o option,
60+
affected by -q""",
61+
action="store_true"
62+
)
63+
parser.add_argument("-b",
64+
"--bib",
65+
help="BibTeX file to look for entries in",
66+
)
67+
return(parser)
5368

69+
def main():
70+
parser = get_parser()
5471
args = parser.parse_args()
5572

56-
# Scan LaTeX files for citations and generate set containing BibTeX entry
57-
# labels
58-
e_set = set()
59-
for file in args.files:
60-
output = subprocess.run([os.path.dirname(os.path.realpath(__file__)) + '/ext.sh',
61-
shlex.quote(file)],
62-
capture_output=True
63-
)
64-
if(output.returncode == 0):
65-
e_set = e_set.union(set(output.stdout.decode().rstrip().split('\n')))
73+
if args.version:
74+
parser.exit(0, f"{VERSION}\n")
75+
else:
76+
if (args.bib is None) or (args.files is None):
77+
parser.print_usage()
78+
parser.exit(2, "Error: the following arguments are required: -b/--bib, -f/--files\n")
79+
else:
80+
# Scan LaTeX files for citations and generate set containing BibTeX entry
81+
# labels
82+
e_set = set()
83+
for file in args.files:
84+
output = subprocess.run([os.path.dirname(os.path.realpath(__file__)) + '/ext.sh',
85+
shlex.quote(file)],
86+
capture_output=True
87+
)
88+
if(output.returncode == 0):
89+
e_set = e_set.union(set(output.stdout.decode().rstrip().split('\n')))
6690

67-
# Merge custom BibTeX entry labels into main set if any
68-
if(args.entries is not None):
69-
with open(args.entries, 'r') as en:
70-
while(entry := en.readline()):
71-
e_set = e_set.union(entry)
91+
# Merge custom BibTeX entry labels into main set if any
92+
if(args.entries is not None):
93+
with open(args.entries, 'r') as en:
94+
while(entry := en.readline()):
95+
e_set = e_set.union(entry)
7296

73-
parser = BibTexParser()
74-
parser.ignore_nonstandard_types = False
75-
with open(args.bib) as bibtex_file:
76-
bib_database = bibtexparser.load(bibtex_file, parser)
97+
parser = BibTexParser()
98+
parser.ignore_nonstandard_types = False
99+
with open(args.bib) as bibtex_file:
100+
bib_database = bibtexparser.load(bibtex_file, parser)
77101

78-
e_dict = bib_database.entries_dict
79-
out_db = BibDatabase()
102+
e_dict = bib_database.entries_dict
103+
out_db = BibDatabase()
80104

81-
entrycount = 0
82-
found = 0
83-
for entry in e_set:
84-
entrycount += 1
85-
if entry in e_dict.keys():
86-
if not args.quiet:
87-
print(f"Found: {entry}")
88-
found += 1
89-
out_db.entries.append(e_dict[entry])
90-
else:
91-
if not args.quiet:
92-
print(f"Not found: {entry}")
105+
entrycount = 0
106+
found = 0
107+
for entry in e_set:
108+
entrycount += 1
109+
if entry in e_dict.keys():
110+
if not args.quiet:
111+
print(f"Found: {entry}")
112+
found += 1
113+
out_db.entries.append(e_dict[entry])
114+
else:
115+
if not args.quiet:
116+
print(f"Not found: {entry}")
93117

94118

95-
writer = BibTexWriter()
96-
writer.indent = args.indent
97-
writer.add_trailing_comma = True
98-
with open(args.output, 'w') as outfile:
99-
bibtexparser.dump(out_db, outfile, writer)
119+
writer = BibTexWriter()
120+
writer.indent = args.indent
121+
writer.add_trailing_comma = True
122+
if not args.dryrun:
123+
with open(args.output, 'w') as outfile:
124+
bibtexparser.dump(out_db, outfile, writer)
100125

101-
if not args.quiet:
102-
print(f"\nFound {found}/{entrycount} entries")
126+
if not args.quiet:
127+
print(f"\nFound {found}/{entrycount} entries")
103128

104129
if __name__ == "__main__":
105130
main()

bibx.1

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ using citations from LaTeX sources, to create project-specific BibTeX
2323
files.
2424
.SH SYNOPSIS
2525
.PP
26-
bibx [-h] -b BIB [-o OUTPUT] [-e ENTRIES] [-q] [-i INDENT] files [files
27-
\&...]
26+
bibx [-h] [-v] [-o OUTPUT] [-e ENTRIES] [-q] [-f FILES [FILES \&...]]
27+
[-i INDENT] [-d] [-b BIB]
2828
.SH DESCRIPTION
2929
.PP
3030
This program extracts citations made in LaTeX source files and generates
@@ -49,31 +49,49 @@ label3
4949
.fi
5050
.SH OPTIONS
5151
.PP
52-
-h, \[en]help
52+
-h, --help
5353
.IP
5454
.nf
5555
\f[C]
5656
Show help and exit
5757
\f[R]
5858
.fi
5959
.PP
60-
-b BIB, \[en]bib BIB
60+
-v, --version
6161
.IP
6262
.nf
6363
\f[C]
64-
Specify global BibTeX file to look for entries in
64+
Show version information and exit
6565
\f[R]
6666
.fi
6767
.PP
68-
-o OUTPUT, \[en]output OUTPUT
68+
-b BIB, --bib BIB
69+
.IP
70+
.nf
71+
\f[C]
72+
Specify global BibTeX file to look for entries in (required if not using -h
73+
or -v)
74+
\f[R]
75+
.fi
76+
.PP
77+
-f FILES [FILES \&...], --files FILES [FILES \&...]
78+
.IP
79+
.nf
80+
\f[C]
81+
LaTeX files to scan for citations whose BibTeX entries need to be extracted
82+
(required if not using -h or -v)
83+
\f[R]
84+
.fi
85+
.PP
86+
-o OUTPUT, --output OUTPUT
6987
.IP
7088
.nf
7189
\f[C]
7290
Specify file to write the BibTeX output to. Defaults to \[aq]./ext.bib\[aq].
7391
\f[R]
7492
.fi
7593
.PP
76-
-e ENTRIES, \[en]entries ENTRIES
94+
-e ENTRIES, --entries ENTRIES
7795
.IP
7896
.nf
7997
\f[C]
@@ -82,7 +100,7 @@ the final output.
82100
\f[R]
83101
.fi
84102
.PP
85-
-i INDENT, \[en]indent INDENT
103+
-i INDENT, --indent INDENT
86104
.IP
87105
.nf
88106
\f[C]
@@ -91,7 +109,16 @@ final output. Defaults to four spaces. Example: -i \[dq] \[dq] (use two spaces)
91109
\f[R]
92110
.fi
93111
.PP
94-
-q, \[en]quiet
112+
-d, --dryrun
113+
.IP
114+
.nf
115+
\f[C]
116+
Perform a dry-run, i.e. do not output to a file, just print info messages.
117+
Ignores the -o option, affected by -q.
118+
\f[R]
119+
.fi
120+
.PP
121+
-q, --quiet
95122
.IP
96123
.nf
97124
\f[C]
@@ -108,7 +135,7 @@ The command to achieve this would be:
108135
.IP
109136
.nf
110137
\f[C]
111-
bibx -b g.bib -o refs.bib a.tex b.tex
138+
bibx -b g.bib -o refs.bib -f a.tex b.tex
112139
\f[R]
113140
.fi
114141
.SH BUGS

bibx.1.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ citations from LaTeX sources, to create project-specific BibTeX files.
77

88
# SYNOPSIS
99

10-
11-
bibx \[-h] -b BIB \[-o OUTPUT] \[-e ENTRIES] \[-q] \[-i INDENT] files \[files ...]
10+
bibx \[-h] \[-v] \[-o OUTPUT] \[-e ENTRIES] \[-q] \[-f FILES [FILES ...]] \[-i INDENT] \[-d] \[-b BIB]
1211

1312
# DESCRIPTION
1413

@@ -31,29 +30,44 @@ label3
3130

3231
# OPTIONS
3332

34-
-h, --help
33+
-h, \--help
3534

3635
Show help and exit
3736

38-
-b BIB, --bib BIB
37+
-v, \--version
38+
39+
Show version information and exit
40+
41+
-b BIB, \--bib BIB
42+
43+
Specify global BibTeX file to look for entries in (required if not using -h
44+
or -v)
3945

40-
Specify global BibTeX file to look for entries in
46+
-f FILES [FILES ...], \--files FILES [FILES ...]
4147

42-
-o OUTPUT, --output OUTPUT
48+
LaTeX files to scan for citations whose BibTeX entries need to be extracted
49+
(required if not using -h or -v)
50+
51+
-o OUTPUT, \--output OUTPUT
4352

4453
Specify file to write the BibTeX output to. Defaults to './ext.bib'.
4554

46-
-e ENTRIES, --entries ENTRIES
55+
-e ENTRIES, \--entries ENTRIES
4756

4857
Specify file containing extra entries names to be searched and included in
4958
the final output.
5059

51-
-i INDENT, --indent INDENT
60+
-i INDENT, \--indent INDENT
5261

5362
Specify the character(s) to be used to indent the BibTeX entries in the
5463
final output. Defaults to four spaces. Example: -i " " (use two spaces).
5564

56-
-q, --quiet
65+
-d, \--dryrun
66+
67+
Perform a dry-run, i.e. do not output to a file, just print info messages.
68+
Ignores the -o option, affected by -q.
69+
70+
-q, \--quiet
5771

5872
Suppress output to stdout
5973

@@ -64,7 +78,7 @@ BibTeX file is `g.bib` and we want the output file to be `refs.bib`.
6478

6579
The command to achieve this would be:
6680
```
67-
bibx -b g.bib -o refs.bib a.tex b.tex
81+
bibx -b g.bib -o refs.bib -f a.tex b.tex
6882
```
6983

7084
# BUGS

0 commit comments

Comments
 (0)