From bec20203e8397e12781b53d8db2daf937e2f3f9b Mon Sep 17 00:00:00 2001 From: Ramprakash C <49452779+CodePurble@users.noreply.github.com> Date: Fri, 14 Oct 2022 00:11:27 +0530 Subject: [PATCH] release `v1.0` * Add `-d/--dryrun` option * LaTeX files provided using `-f/--files` option instead of positional args * Add `-v/--version` option --- CHANGELOG.md | 13 ++++-- README.md | 6 +-- TOOD.md | 2 +- bibx | 119 +++++++++++++++++++++++++++++++-------------------- bibx.1 | 47 +++++++++++++++----- bibx.1.md | 34 ++++++++++----- 6 files changed, 147 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff9dcf6..ebb3576 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -8,4 +14,5 @@ ## Feature additions * Add option to specify a custom install prefix - +# v0.1 +* Initial release diff --git a/README.md b/README.md index fcf9282..0175f14 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/TOOD.md b/TOOD.md index 850b292..42d7c18 100644 --- a/TOOD.md +++ b/TOOD.md @@ -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) diff --git a/bibx b/bibx index 133317d..3e6acc0 100755 --- a/bibx +++ b/bibx @@ -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", @@ -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="+" ) @@ -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() diff --git a/bibx.1 b/bibx.1 index 336160d..b4eb127 100644 --- a/bibx.1 +++ b/bibx.1 @@ -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 @@ -49,7 +49,7 @@ label3 .fi .SH OPTIONS .PP --h, \[en]help +-h, --help .IP .nf \f[C] @@ -57,15 +57,33 @@ 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] @@ -73,7 +91,7 @@ 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] @@ -82,7 +100,7 @@ the final output. \f[R] .fi .PP --i INDENT, \[en]indent INDENT +-i INDENT, --indent INDENT .IP .nf \f[C] @@ -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] @@ -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 diff --git a/bibx.1.md b/bibx.1.md index 700a882..f5e1421 100644 --- a/bibx.1.md +++ b/bibx.1.md @@ -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 @@ -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 @@ -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