twilsonco / latexdiffcite

latexdiffcite is a wrapper around latexdiff to make citations diff properly
BSD 2-Clause "Simplified" License
26 stars 9 forks source link

AttributeError: 'NoneType' object has no attribute 'group' #12

Open inoue0426 opened 1 year ago

inoue0426 commented 1 year ago

Hi,

I got this error, maybe related to parse error.

processing old revision
Traceback (most recent call last):
  File "/Users/yoshitakainoue/.pyenv/versions/miniforge3-4.10.1-5/envs/py310/bin/latexdiffcite", line 8, in <module>
    sys.exit(main())
  File "/Users/yoshitakainoue/.pyenv/versions/miniforge3-4.10.1-5/envs/py310/lib/python3.10/site-packages/latexdiffcite/latexdiffcite.py", line 163, in main
    run(parsed_args)
  File "/Users/yoshitakainoue/.pyenv/versions/miniforge3-4.10.1-5/envs/py310/lib/python3.10/site-packages/latexdiffcite/latexdiffcite.py", line 296, in run
    process_revision('old')
  File "/Users/yoshitakainoue/.pyenv/versions/miniforge3-4.10.1-5/envs/py310/lib/python3.10/site-packages/latexdiffcite/latexdiffcite.py", line 361, in process_revision
    read_bibfile(oldnew)
  File "/Users/yoshitakainoue/.pyenv/versions/miniforge3-4.10.1-5/envs/py310/lib/python3.10/site-packages/latexdiffcite/latexdiffcite.py", line 481, in read_bibfile
    bibarg = find_bibliography_arg(getattr(FileContents, 'tex_' + oldnew))
  File "/Users/yoshitakainoue/.pyenv/versions/miniforge3-4.10.1-5/envs/py310/lib/python3.10/site-packages/latexdiffcite/latexdiffcite.py", line 496, in find_bibliography_arg
    bibfile = re.search(r'$[^%]*\\bibliography\s*{(.*?)}', s, flags=re.M).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Do you know how to fix this?

tobiscode commented 3 months ago

Hi,

this is a result of the regex search not finding any linked bibliography files in the tex file. This can be due to other commands being used to specify the bibliography files, e.g., \addbibresource instead of \bibliography.

One way out is to specify to use the BBL file directly using the --bbl option explicitly. latexdiffcite file -h is a bit ambiguous here, as the default is actually to not look for one, but setting it to the working directory should work.

The (in my opinion) better alternative is to fix the latexdiffcite.py script to simply look for those other commands. Here's what I changed in my local version to make everything work again, including with multiple files:

@@ -478,8 +478,9 @@ def read_bibfile(oldnew):
     '''Reads contents of bibtex files'''
     # get bibtex file
-    bibarg = find_bibliography_arg(getattr(FileContents, 'tex_' + oldnew))
-    find_bibfiles(bibarg, oldnew)
+    bibargs = find_bibliography_args(getattr(FileContents, 'tex_' + oldnew))
+    for bibarg in bibargs:
+        find_bibfiles(bibarg, oldnew)
     bibfiles = getattr(Files, 'bib_' + oldnew + '_path')
     # read bibtex files
@@ -489,13 +490,16 @@ def read_bibfile(oldnew):
             getattr(FileContents, 'bib_' + oldnew).append(f.read())
-def find_bibliography_arg(s):
+def find_bibliography_args(s):
     '''Looks through string for \bibliography{} command and retrieves the argument'''
+    bibfiles = []
     log.debug('searching for \\bibliography{} entry in tex file')
-    bibfile = re.search(r'$[^%]*\\bibliography\s*{(.*?)}', s, flags=re.M).group(1)
-    log.debug('bibliography argument found: %s', bibfile)
-    return bibfile
+    bibfiles.extend(re.findall(r'^\s*\\bibliography\s*{(.*?)}', s, flags=re.M))
+    log.debug('searching for \\addbibresource{} entries in tex file')
+    bibfiles.extend(re.findall(r'^\s*\\addbibresource\s*{(.*?)}', s, flags=re.M))
+    log.debug('bibliography arguments found: %s', str(bibfiles))
+    return bibfiles

Hope that helps! Happy to submit a PR if it's welcome @twilsonco @cmeeren .

Cheers

twilsonco commented 3 months ago

Thanks @tobiscode,

I agree the best approach is to modify the source to check for all applicable bibliography commands.

I’ll be using this tool again for some paper revisions soon, and will update things then.

A PR would be welcome since you already have the changes working.

Thanks!