Skip to content

Commit

Permalink
release v1.0
Browse files Browse the repository at this point in the history
* Add `-d/--dryrun` option
* LaTeX files provided using `-f/--files` option instead of positional args
* Add `-v/--version` option
  • Loading branch information
CodePurble committed Oct 13, 2022
1 parent c07ef60 commit bec2020
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 74 deletions.
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# v0.1
* Initial release
# v1.0
## Breaking changes
* LaTeX source files are now provided using the `-f/--files` option rather than
as positional arguments

## Feature additions
* Add dry-run option `-d/--dryrun`
* Add version option `-v/--version`

# v0.12
## Fixes
Expand All @@ -8,4 +14,5 @@
## Feature additions
* Add option to specify a custom install prefix


# v0.1
* Initial release
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ Assume that there are two LaTeX files: `a.tex` and `b.tex`, the reference
BibTeX file is `g.bib` and we want the output file to be `refs.bib`. The
command to achieve this would be:
```sh
bibx -b g.bib -o refs.bib a.tex b.tex
bibx -b g.bib -o refs.bib -f a.tex b.tex

# A variation
bibx -b g.bib -o refs.bib *.tex
bibx -b g.bib -o refs.bib -f *.tex
```

## Bugs
Expand All @@ -84,7 +84,7 @@ pandoc --standalone --to man bibx.1.md -o bibx.1
### Tests
If contributing to the code, please run the following test after your edits:
```
./bibx -b test/global.bib -o test/ext.bib test/*.tex
./bibx -b test/global.bib -o test/ext.bib -f test/*.tex
diff test/ext.bib test/ext-golden.bib
```
The `diff` command must return **NOTHING**. This means that the output is as
Expand Down
2 changes: 1 addition & 1 deletion TOOD.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- [ ] Remove dependency on `ext.sh`
- [ ] Support multiple reference BibTeX files
- [ ] Support a directory of reference BibTeX files
- [ ] Option to perform a dry-run (no file output, only print info to stdout)
- [x] Option to perform a dry-run (no file output, only print info to stdout)
119 changes: 72 additions & 47 deletions bibx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import os
from bibtexparser.bparser import BibTexParser, BibDatabase
from bibtexparser.bwriter import BibTexWriter

def main():
VERSION = "v1.0"

def get_parser():
parser = argparse.ArgumentParser(
description="""Extract bibliography entries from LaTeX sources
using a reference BibTeX file""",
epilog="""For reporting bugs, giving suggestions and contributing
to this project, visit https://github.com/CodePurble/bibextract"""
)
parser.add_argument("-b",
"--bib",
required=True,
help="BibTeX file to look for entries in"
parser.add_argument("-v",
"--version",
help="Show version information and exit",
action="store_true"
)
parser.add_argument("-o",
"--output",
Expand All @@ -38,9 +40,10 @@ def main():
help="Suppress output to stdout",
action="store_true"
)
parser.add_argument("files",
parser.add_argument("-f",
"--files",
help="""LaTeX files to scan for citations whose BibTeX
entries need to be extract""",
entries need to be extracted""",
action="extend",
nargs="+"
)
Expand All @@ -50,56 +53,78 @@ def main():
file. Default is four spaces""",
default=" ",
)
parser.add_argument("-d",
"--dryrun",
help="""Perform a dry-run, i.e. do not output a file,
just print info messages. Ignores the -o option,
affected by -q""",
action="store_true"
)
parser.add_argument("-b",
"--bib",
help="BibTeX file to look for entries in",
)
return(parser)

def main():
parser = get_parser()
args = parser.parse_args()

# Scan LaTeX files for citations and generate set containing BibTeX entry
# labels
e_set = set()
for file in args.files:
output = subprocess.run([os.path.dirname(os.path.realpath(__file__)) + '/ext.sh',
shlex.quote(file)],
capture_output=True
)
if(output.returncode == 0):
e_set = e_set.union(set(output.stdout.decode().rstrip().split('\n')))
if args.version:
parser.exit(0, f"{VERSION}\n")
else:
if (args.bib is None) or (args.files is None):
parser.print_usage()
parser.exit(2, "Error: the following arguments are required: -b/--bib, -f/--files\n")
else:
# Scan LaTeX files for citations and generate set containing BibTeX entry
# labels
e_set = set()
for file in args.files:
output = subprocess.run([os.path.dirname(os.path.realpath(__file__)) + '/ext.sh',
shlex.quote(file)],
capture_output=True
)
if(output.returncode == 0):
e_set = e_set.union(set(output.stdout.decode().rstrip().split('\n')))

# Merge custom BibTeX entry labels into main set if any
if(args.entries is not None):
with open(args.entries, 'r') as en:
while(entry := en.readline()):
e_set = e_set.union(entry)
# Merge custom BibTeX entry labels into main set if any
if(args.entries is not None):
with open(args.entries, 'r') as en:
while(entry := en.readline()):
e_set = e_set.union(entry)

parser = BibTexParser()
parser.ignore_nonstandard_types = False
with open(args.bib) as bibtex_file:
bib_database = bibtexparser.load(bibtex_file, parser)
parser = BibTexParser()
parser.ignore_nonstandard_types = False
with open(args.bib) as bibtex_file:
bib_database = bibtexparser.load(bibtex_file, parser)

e_dict = bib_database.entries_dict
out_db = BibDatabase()
e_dict = bib_database.entries_dict
out_db = BibDatabase()

entrycount = 0
found = 0
for entry in e_set:
entrycount += 1
if entry in e_dict.keys():
if not args.quiet:
print(f"Found: {entry}")
found += 1
out_db.entries.append(e_dict[entry])
else:
if not args.quiet:
print(f"Not found: {entry}")
entrycount = 0
found = 0
for entry in e_set:
entrycount += 1
if entry in e_dict.keys():
if not args.quiet:
print(f"Found: {entry}")
found += 1
out_db.entries.append(e_dict[entry])
else:
if not args.quiet:
print(f"Not found: {entry}")


writer = BibTexWriter()
writer.indent = args.indent
writer.add_trailing_comma = True
with open(args.output, 'w') as outfile:
bibtexparser.dump(out_db, outfile, writer)
writer = BibTexWriter()
writer.indent = args.indent
writer.add_trailing_comma = True
if not args.dryrun:
with open(args.output, 'w') as outfile:
bibtexparser.dump(out_db, outfile, writer)

if not args.quiet:
print(f"\nFound {found}/{entrycount} entries")
if not args.quiet:
print(f"\nFound {found}/{entrycount} entries")

if __name__ == "__main__":
main()
47 changes: 37 additions & 10 deletions bibx.1
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ using citations from LaTeX sources, to create project-specific BibTeX
files.
.SH SYNOPSIS
.PP
bibx [-h] -b BIB [-o OUTPUT] [-e ENTRIES] [-q] [-i INDENT] files [files
\&...]
bibx [-h] [-v] [-o OUTPUT] [-e ENTRIES] [-q] [-f FILES [FILES \&...]]
[-i INDENT] [-d] [-b BIB]
.SH DESCRIPTION
.PP
This program extracts citations made in LaTeX source files and generates
Expand All @@ -49,31 +49,49 @@ label3
.fi
.SH OPTIONS
.PP
-h, \[en]help
-h, --help
.IP
.nf
\f[C]
Show help and exit
\f[R]
.fi
.PP
-b BIB, \[en]bib BIB
-v, --version
.IP
.nf
\f[C]
Specify global BibTeX file to look for entries in
Show version information and exit
\f[R]
.fi
.PP
-o OUTPUT, \[en]output OUTPUT
-b BIB, --bib BIB
.IP
.nf
\f[C]
Specify global BibTeX file to look for entries in (required if not using -h
or -v)
\f[R]
.fi
.PP
-f FILES [FILES \&...], --files FILES [FILES \&...]
.IP
.nf
\f[C]
LaTeX files to scan for citations whose BibTeX entries need to be extracted
(required if not using -h or -v)
\f[R]
.fi
.PP
-o OUTPUT, --output OUTPUT
.IP
.nf
\f[C]
Specify file to write the BibTeX output to. Defaults to \[aq]./ext.bib\[aq].
\f[R]
.fi
.PP
-e ENTRIES, \[en]entries ENTRIES
-e ENTRIES, --entries ENTRIES
.IP
.nf
\f[C]
Expand All @@ -82,7 +100,7 @@ the final output.
\f[R]
.fi
.PP
-i INDENT, \[en]indent INDENT
-i INDENT, --indent INDENT
.IP
.nf
\f[C]
Expand All @@ -91,7 +109,16 @@ final output. Defaults to four spaces. Example: -i \[dq] \[dq] (use two spaces)
\f[R]
.fi
.PP
-q, \[en]quiet
-d, --dryrun
.IP
.nf
\f[C]
Perform a dry-run, i.e. do not output to a file, just print info messages.
Ignores the -o option, affected by -q.
\f[R]
.fi
.PP
-q, --quiet
.IP
.nf
\f[C]
Expand All @@ -108,7 +135,7 @@ The command to achieve this would be:
.IP
.nf
\f[C]
bibx -b g.bib -o refs.bib a.tex b.tex
bibx -b g.bib -o refs.bib -f a.tex b.tex
\f[R]
.fi
.SH BUGS
Expand Down
34 changes: 24 additions & 10 deletions bibx.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ citations from LaTeX sources, to create project-specific BibTeX files.

# SYNOPSIS


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

# DESCRIPTION

Expand All @@ -31,29 +30,44 @@ label3

# OPTIONS

-h, --help
-h, \--help

Show help and exit

-b BIB, --bib BIB
-v, \--version

Show version information and exit

-b BIB, \--bib BIB

Specify global BibTeX file to look for entries in (required if not using -h
or -v)

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

-o OUTPUT, --output OUTPUT
LaTeX files to scan for citations whose BibTeX entries need to be extracted
(required if not using -h or -v)

-o OUTPUT, \--output OUTPUT

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

-e ENTRIES, --entries ENTRIES
-e ENTRIES, \--entries ENTRIES

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

-i INDENT, --indent INDENT
-i INDENT, \--indent INDENT

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

-q, --quiet
-d, \--dryrun

Perform a dry-run, i.e. do not output to a file, just print info messages.
Ignores the -o option, affected by -q.

-q, \--quiet

Suppress output to stdout

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

The command to achieve this would be:
```
bibx -b g.bib -o refs.bib a.tex b.tex
bibx -b g.bib -o refs.bib -f a.tex b.tex
```

# BUGS
Expand Down

0 comments on commit bec2020

Please sign in to comment.